Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.IO
Public Class Form1
    Dim connect As New Connection()
    Dim TDT As String = Format(Now.Date, "yyyyMMdd")
    Dim Cdt As String = "2014-02-01"
    Dim StartDt As String = "2014-02-01"
    Private Sub Button8_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Exit Sub
            End If
            '  If PublicVariable.oCompany.Connected = False Then
            connect.setDB()
            Dim st As String = connect.connectDB()
            If st <> "" Then
                'MsgBox(st)
                Exit Sub
            End If
            '  End If

            Dim k As Integer = 0
            Dim oRecordSet8 As SAPbobsCOM.Recordset
            Dim oRecordSet7 As SAPbobsCOM.Recordset
            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet8 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet8.DoQuery("SELECT T0.[CardCode] FROM OCRD T0 WHERE T0.[CardType] ='C'")
            Dim i As Integer = 0
            For i = 1 To oRecordSet8.RecordCount

                oRecordSet7 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                Dim PrimKey As String = oRecordSet8.Fields.Item("CardCode").Value
                Dim str As String = "SELECT T0.[CardCode],T0.DocDate, T1.[Quantity],T1.[U_AI_PetCode] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104' or (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.CardCode ='" & PrimKey & "' order by T0.DocEntry Desc"
                oRecordSet7.DoQuery(str)
                If oRecordSet7.RecordCount > 0 Then
                    k = k + 1
                    'Label1.Text = oRecordSet8.RecordCount & " of " & i & "-Record Processing"
                    Dim Dt As Date = Format(oRecordSet7.Fields.Item("DocDate").Value, "yyyy-MM-dd")
                    Dt = Dt.AddMonths(6)
                    oRecordSet6 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet6.DoQuery("Update OCRD Set [U_AI_ExpDate]='" & Format(Dt, "yyyy-MM-dd") & "'  WHERE [CardCode] ='" & PrimKey & "'")
                End If
                oRecordSet8.MoveNext()
            Next
            'MsgBox("Complected")
            PublicVariable.oCompany.Disconnect()
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Exit Sub
            End If

            connect.setDB()
            Dim st As String = connect.connectDB()
            If st <> "" Then
                'MsgBox(st)
                Exit Sub
            End If


            Dim k As Integer = 0
            Dim oRecordSet8 As SAPbobsCOM.Recordset
            Dim oRecordSet7 As SAPbobsCOM.Recordset
            Dim oRecordSet6 As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet8 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet8.DoQuery("SELECT T0.[U_PetCode], T0.[U_PetName], T0.[U_AI_CustCode], T0.[U_AI_CustName], T0.[U_AI_Addr] FROM [dbo].[@AI_PETMASTER]  T0")
            Dim i As Integer = 0
            For i = 1 To oRecordSet8.RecordCount

                oRecordSet7 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                Dim BPCode As String = oRecordSet8.Fields.Item("U_AI_CustCode").Value
                Dim PetCode As String = oRecordSet8.Fields.Item("U_PetCode").Value
                oRecordSet1.DoQuery("SELECT cast(isnull(T0.[Address],'') as varchar(500)) + ' ' + cast(isnull(T0.[StreetNo],'') as varchar(500)) + ' ' + cast(isnull(T0.[Block],'') as Varchar(100)) + ' ' + cast(isnull(T0.[Building],'') as Varchar(600)) + ' ' + cast(isnull(T0.City,'') as Varchar(600))+ ' '    + Cast(isnull(T1.[Name],'') + ' ' + Cast( isnull(T0.[ZipCode],'') as varchar(100)) as Varchar(100)) FROM [dbo].[OCRD]  T0 INNER JOIN OCRY T1 ON T0.Country = T1.Code WHERE T0.[CardCode]  ='" & BPCode & "'")
                Dim BPAddr As String = oRecordSet1.Fields.Item(0).Value '& "  " & oRecordSet1.Fields.Item(1).Value & vbCrLf & oRecordSet1.Fields.Item(2).Value & "  " & oRecordSet1.Fields.Item(3).Value
                BPAddr = BPAddr.Replace("'", " ")
                If oRecordSet1.RecordCount > 0 Then
                    k = k + 1
                    oRecordSet6 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    Dim st1 As String = "update  [dbo].[@AI_PETMASTER]  set [U_AI_Addr]='" & BPAddr & "' where [U_PetCode]='" & PetCode & "'"
                    oRecordSet6.DoQuery(st1)
                End If
                oRecordSet8.MoveNext()
            Next
            'MsgBox("Complected")
            PublicVariable.oCompany.Disconnect()
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Exit Sub
            End If
            '  If PublicVariable.oCompany.Connected = False Then
            connect.setDB()
            Dim st As String = connect.connectDB()
            If st <> "" Then
                'MsgBox(st)
                Exit Sub
            End If
            '  End If

            Dim k As Integer = 0
            Dim oRecordSet8 As SAPbobsCOM.Recordset
            Dim oRecordSet7 As SAPbobsCOM.Recordset
            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet8 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet8.DoQuery("SELECT T0.[CardCode] FROM OCRD T0 WHERE T0.[CardType] ='C'")
            Dim i As Integer = 0
            For i = 1 To oRecordSet8.RecordCount

                oRecordSet7 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                Dim PrimKey As String = oRecordSet8.Fields.Item("CardCode").Value
                Dim str As String = "SELECT T0.[CardCode],T0.DocDate, T1.[Quantity],T1.[U_AI_PetCode] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode]))='103' and T0.CardCode ='" & PrimKey & "' order by T0.DocEntry Desc"
                oRecordSet7.DoQuery(str)
                If oRecordSet7.RecordCount > 0 Then
                    k = k + 1
                    'Label1.Text = oRecordSet8.RecordCount & " of " & i & "-Record Processing"
                    Dim Dt As Date = Format(oRecordSet7.Fields.Item("DocDate").Value, "yyyy-MM-dd")
                    Dt = Dt.AddMonths(6)
                    oRecordSet6 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet6.DoQuery("Update OCRD Set [U_AI_BoardExp]='" & Format(Dt, "yyyy-MM-dd") & "'  WHERE [CardCode] ='" & PrimKey & "'")
                End If
                oRecordSet8.MoveNext()
            Next
            'MsgBox("Complected")
            PublicVariable.oCompany.Disconnect()
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Exit Sub
            End If
            '  If PublicVariable.oCompany.Connected = False Then
            connect.setDB()
            Dim st As String = connect.connectDB()
            If st <> "" Then
                'MsgBox(st)
                Exit Sub
            End If
            '  End If

            Dim k As Integer = 0
            Dim oRecordSet8 As SAPbobsCOM.Recordset
            Dim oRecordSet7 As SAPbobsCOM.Recordset
            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet8 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet8.DoQuery("SELECT distinct T0.[U_AI_PetCode] 'U_AI_PetCode' FROM [dbo].[@AI_ATTENDANCE]  T0 WHERE isnull( T0.[U_AI_PetCode] ,'')<>''")
            Dim i As Integer = 0
            For i = 1 To oRecordSet8.RecordCount

                oRecordSet7 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                Dim PrimKey As String = oRecordSet8.Fields.Item("U_AI_PetCode").Value
                Dim str As String = "SELECT max(T0.[U_AI_Date]) as 'U_AI_Date' FROM [dbo].[@AI_ATTENDANCE]  T0 WHERE T0.[U_AI_PetCode] ='" & PrimKey & "'"
                oRecordSet7.DoQuery(str)
                If oRecordSet7.RecordCount > 0 Then
                    k = k + 1
                    'Label1.Text = oRecordSet8.RecordCount & " of " & i & "-Record Processing"
                    Dim Dt As Date = Format(oRecordSet7.Fields.Item("U_AI_Date").Value, "yyyy-MM-dd")
                    '  Dt = Dt.AddMonths(6)
                    oRecordSet6 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet6.DoQuery("UPDATE [dbo].[@AI_PETMASTER] SET  [U_AI_LastVisit] ='" & Format(Dt, "yyyy-MM-dd") & "' WHERE [U_PetCode] ='" & PrimKey & "'")
                    oRecordSet6.DoQuery("UPDATE [dbo].[OCRD] SET  [U_AI_LastVisit] ='" & Format(Dt, "yyyy-MM-dd") & "' WHERE CardCode=(select top 1 U_AI_CustCode from [@AI_PetMaster] where U_PetCode= '" & PrimKey & "')")
                End If
                oRecordSet8.MoveNext()
            Next
            'MsgBox("Complected")
            PublicVariable.oCompany.Disconnect()
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Exit Sub
            End If
            connect.setDB()
            Dim st As String = connect.connectDB()
            If st <> "" Then
                'MsgBox(st)
                Exit Sub
            End If
            Dim k As Integer = 0
            Dim PetCode As String = ""
            Dim oRecordSet8 As SAPbobsCOM.Recordset
            Dim oRecordSet7 As SAPbobsCOM.Recordset
            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet8 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet8.DoQuery("SELECT T0.[CreateDate], T0.[U_PetCode] FROM [dbo].[@AI_EVALUATION]  T0 ORDER BY T0.[DocEntry]")
            Dim i As Integer = 0
            For i = 1 To oRecordSet8.RecordCount
                PetCode = oRecordSet8.Fields.Item("U_PetCode").Value
                Dim dt As String = Format(oRecordSet8.Fields.Item("CreateDate").Value, "yyyyMMdd")
                oRecordSet6 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet6.DoQuery("UPDATE [dbo].[@AI_PETMASTER] SET  [U_AI_EvalDate] ='" & dt & "' WHERE [U_PetCode] ='" & PetCode & "'")
                oRecordSet8.MoveNext()
            Next
            'MsgBox("Complected")
            PublicVariable.oCompany.Disconnect()
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub JV_INVOICE_DAYPASS(ByVal sdt As String, ByVal sdt1 As String, ByVal Dt1 As Date)
        Try

            Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            Dim firstDayLastMonth As DateTime
            Dim lastDayLastMonth As DateTime
            Dim firstDayThisMonth As DateTime
            'Dim sdt As String
            'Dim sdt1 As String
            firstDayLastMonth = thisMonth.AddMonths(-1)
            firstDayThisMonth = thisMonth.AddMonths(0)
            lastDayLastMonth = thisMonth.AddDays(-1)
            'sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
            'sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            oRecordSet4 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

            oRecordSet.DoQuery("SELECT (T1.[Quantity]) Quantity ,isnull(T1.U_FreeDP,0) U_FreeDP,T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104'   and  T0.DocDate between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T1.U_St,'') <> 'Closed'")

            Dim i As Integer = 0
            Dim J As Integer = 0
            'Dim oJV As SAPbobsCOM.JournalVouchers
            'oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)
            'SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',
            'T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103'   and  T0.DocDate  between '" & sdt & "' and '" & sdt1 & "'")
            Dim docnum As String = ""
            For i = 1 To oRecordSet.RecordCount
                '------------------------
                Try
                    Dim SPDog As String = ""
                    SPDog = SpecialDogs(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany)
                    If SPDog <> "Yes" Then
                        For J = 1 To oRecordSet.Fields.Item("Quantity").Value
                            Dim oGeneralService As SAPbobsCOM.GeneralService
                            Dim oGeneralData As SAPbobsCOM.GeneralData
                            Dim oSons As SAPbobsCOM.GeneralDataCollection
                            Dim oSon As SAPbobsCOM.GeneralData
                            Dim sCmp As SAPbobsCOM.CompanyService
                            sCmp = PublicVariable.oCompany.GetCompanyService
                            oGeneralService = sCmp.GetGeneralService("INVOICEOB")
                            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                            oGeneralData.SetProperty("U_Type", "Invoice")
                            oGeneralData.SetProperty("U_SubType", "DAYPASS")
                            oGeneralData.SetProperty("U_InvDate", oRecordSet.Fields.Item("DocDate").Value)
                            oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                            oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                            oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                            oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                            oGeneralData.SetProperty("U_Count", 1)
                            oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_InvNo", oRecordSet.Fields.Item("DocEntry").Value)
                            oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value)
                            oGeneralData.SetProperty("U_UnitPrice", oRecordSet.Fields.Item("PriceBefDi").Value)
                            oGeneralService.Add(oGeneralData)
                        Next
                        For J = 1 To oRecordSet.Fields.Item("U_FreeDP").Value
                            Dim oGeneralService As SAPbobsCOM.GeneralService
                            Dim oGeneralData As SAPbobsCOM.GeneralData
                            Dim oSons As SAPbobsCOM.GeneralDataCollection
                            Dim oSon As SAPbobsCOM.GeneralData
                            Dim sCmp As SAPbobsCOM.CompanyService
                            sCmp = PublicVariable.oCompany.GetCompanyService
                            oGeneralService = sCmp.GetGeneralService("INVOICEOB")
                            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                            oGeneralData.SetProperty("U_Type", "Invoice")
                            oGeneralData.SetProperty("U_SubType", "DAYPASS")
                            oGeneralData.SetProperty("U_InvDate", oRecordSet.Fields.Item("DocDate").Value)
                            oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                            oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                            oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                            oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                            oGeneralData.SetProperty("U_Count", 1)
                            oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_InvNo", oRecordSet.Fields.Item("DocEntry").Value)
                            oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value)
                            'oGeneralData.SetProperty("U_UnitPrice", oRecordSet.Fields.Item("PriceBefDi").Value)
                            oGeneralService.Add(oGeneralData)
                        Next
                    End If
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE INV1 SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & oRecordSet.Fields.Item("LineNum").Value & "'")
                oRecordSet.MoveNext()
            Next


            oRecordSet.DoQuery("SELECT T0.[U_Date],T0.[Code], T0.[Name], T0.[U_Petcode], T0.[U_PeName], T0.[U_CardCode], T0.[U_DP] FROM [dbo].[@OPENINGBALANCE]  T0 WHERE T0.[U_DP] > 0 and  T0.[U_Date]  between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T0.U_St2,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                '------------------------
                Try
                    Dim SPDog As String = ""
                    SPDog = SpecialDogs(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany)
                    If SPDog <> "Yes" Then
                        For J = 1 To oRecordSet.Fields.Item("U_DP").Value
                            Dim oGeneralService As SAPbobsCOM.GeneralService
                            Dim oGeneralData As SAPbobsCOM.GeneralData
                            Dim oSons As SAPbobsCOM.GeneralDataCollection
                            Dim oSon As SAPbobsCOM.GeneralData
                            Dim sCmp As SAPbobsCOM.CompanyService
                            sCmp = PublicVariable.oCompany.GetCompanyService
                            oGeneralService = sCmp.GetGeneralService("INVOICEOB")
                            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                            oGeneralData.SetProperty("U_Type", "OB")
                            oGeneralData.SetProperty("U_SubType", "DAYPASS")
                            oGeneralData.SetProperty("U_InvDate", oRecordSet.Fields.Item("U_Date").Value)
                            oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_Petcode").Value)
                            oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_PeName").Value)
                            oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("U_CardCode").Value)
                            oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_Count", 1)
                            oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_InvNo", oRecordSet.Fields.Item("Code").Value)
                            'oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("Code").Value)
                            'oGeneralData.SetProperty("U_UnitPrice", oRecordSet.Fields.Item("PriceBefDi").Value)
                            oGeneralService.Add(oGeneralData)
                        Next
                    End If
                Catch ex As Exception
                    ''MsgBox(ex.Message)
                End Try
                oRecordSet2.DoQuery("UPDATE [@OPENINGBALANCE] SET U_ST2='Closed' where Code='" & oRecordSet.Fields.Item("Code").Value & "'")
                oRecordSet.MoveNext()
            Next


            ''MsgBox("Finish")
            '-----------------End Transport--------

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub JV_INVOICE_TRANSPORT(ByVal sdt As String, ByVal sdt1 As String, ByVal Dt1 As Date)
        Try



            'Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            'Dim firstDayLastMonth As DateTime
            'Dim lastDayLastMonth As DateTime
            'Dim firstDayThisMonth As DateTime
            ''Dim sdt As String
            ''Dim sdt1 As String
            'firstDayLastMonth = thisMonth.AddMonths(-1)
            'firstDayThisMonth = thisMonth.AddMonths(0)
            'lastDayLastMonth = thisMonth.AddDays(-1)
            'sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
            'sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim SPDog As String = ""
            oRecordSet.DoQuery("SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='110'   and  T0.DocDate between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T1.U_St,'') <> 'Closed'")
            Dim i As Integer = 0
            Dim J As Integer = 0
            'Dim oJV As SAPbobsCOM.JournalVouchers
            'oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)
            'SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',
            'T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103'   and  T0.DocDate  between '" & sdt & "' and '" & sdt1 & "'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    SPDog = SpecialDogs(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany)
                    If SPDog <> "Yes" Then
                        For J = 1 To oRecordSet.Fields.Item("Quantity").Value
                            Dim oGeneralService As SAPbobsCOM.GeneralService
                            Dim oGeneralData As SAPbobsCOM.GeneralData
                            Dim oSons As SAPbobsCOM.GeneralDataCollection
                            Dim oSon As SAPbobsCOM.GeneralData
                            Dim sCmp As SAPbobsCOM.CompanyService
                            sCmp = PublicVariable.oCompany.GetCompanyService
                            oGeneralService = sCmp.GetGeneralService("INVOICEOB")
                            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                            oGeneralData.SetProperty("U_Type", "Invoice")
                            oGeneralData.SetProperty("U_SubType", "TRANSPORT")
                            oGeneralData.SetProperty("U_InvDate", oRecordSet.Fields.Item("DocDate").Value)
                            oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                            oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                            oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                            oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                            oGeneralData.SetProperty("U_Count", 1)
                            oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_InvNo", oRecordSet.Fields.Item("DocEntry").Value)
                            oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value)
                            oGeneralData.SetProperty("U_UnitPrice", oRecordSet.Fields.Item("PriceBefDi").Value)
                            oGeneralService.Add(oGeneralData)
                        Next
                    End If
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE INV1 SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & oRecordSet.Fields.Item("LineNum").Value & "'")
                oRecordSet.MoveNext()
            Next
            '---------Tow Way------------
            oRecordSet.DoQuery("SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='109'   and  T0.DocDate between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T1.U_St,'') <> 'Closed'")
            'Dim i As Integer = 0
            'Dim J As Integer = 0
            'Dim oJV As SAPbobsCOM.JournalVouchers
            'oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)
            'SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',
            'T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103'   and  T0.DocDate  between '" & sdt & "' and '" & sdt1 & "'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    SPDog = SpecialDogs(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany)
                    If SPDog <> "Yes" Then
                        For J = 1 To (oRecordSet.Fields.Item("Quantity").Value * 2)
                            Dim oGeneralService As SAPbobsCOM.GeneralService
                            Dim oGeneralData As SAPbobsCOM.GeneralData
                            Dim oSons As SAPbobsCOM.GeneralDataCollection
                            Dim oSon As SAPbobsCOM.GeneralData
                            Dim sCmp As SAPbobsCOM.CompanyService
                            sCmp = PublicVariable.oCompany.GetCompanyService
                            oGeneralService = sCmp.GetGeneralService("INVOICEOB")
                            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                            oGeneralData.SetProperty("U_Type", "Invoice")
                            oGeneralData.SetProperty("U_SubType", "TRANSPORT")
                            oGeneralData.SetProperty("U_InvDate", oRecordSet.Fields.Item("DocDate").Value)
                            oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                            oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                            oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                            oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                            oGeneralData.SetProperty("U_Count", 1)
                            oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_InvNo", oRecordSet.Fields.Item("DocEntry").Value)
                            oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value)
                            oGeneralData.SetProperty("U_UnitPrice", (oRecordSet.Fields.Item("PriceBefDi").Value / 2))
                            oGeneralService.Add(oGeneralData)
                        Next
                    End If
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE INV1 SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & oRecordSet.Fields.Item("LineNum").Value & "'")
                oRecordSet.MoveNext()
            Next

            oRecordSet.DoQuery("SELECT T0.[U_Date],T0.[Code], T0.[Name], T0.[U_Petcode], T0.[U_PeName], T0.[U_CardCode], T0.[U_TRNSBAL] FROM [dbo].[@OPENINGBALANCE]  T0 WHERE T0.[U_TRNSBAL] > 0 and  T0.[U_Date]  between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T0.U_St,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                '------------------------
                Try
                    SPDog = SpecialDogs(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany)
                    If SPDog <> "Yes" Then
                        For J = 1 To oRecordSet.Fields.Item("U_TRNSBAL").Value
                            Dim oGeneralService As SAPbobsCOM.GeneralService
                            Dim oGeneralData As SAPbobsCOM.GeneralData
                            Dim oSons As SAPbobsCOM.GeneralDataCollection
                            Dim oSon As SAPbobsCOM.GeneralData
                            Dim sCmp As SAPbobsCOM.CompanyService
                            sCmp = PublicVariable.oCompany.GetCompanyService
                            oGeneralService = sCmp.GetGeneralService("INVOICEOB")
                            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                            oGeneralData.SetProperty("U_Type", "OB")
                            oGeneralData.SetProperty("U_SubType", "TRANSPORT")
                            oGeneralData.SetProperty("U_InvDate", oRecordSet.Fields.Item("U_Date").Value)
                            oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_Petcode").Value)
                            oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_PeName").Value)
                            oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("U_CardCode").Value)
                            oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_Count", 1)
                            oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_InvNo", oRecordSet.Fields.Item("Code").Value)
                            'oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("Code").Value)
                            'oGeneralData.SetProperty("U_UnitPrice", oRecordSet.Fields.Item("PriceBefDi").Value)
                            oGeneralService.Add(oGeneralData)
                        Next
                    End If
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE [@OPENINGBALANCE] SET U_ST='Closed' where Code='" & oRecordSet.Fields.Item("Code").Value & "'")
                oRecordSet.MoveNext()
            Next
            '''MsgBox("Finish")
            '-----------------End Transport--------

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub JV_INVOICE(ByVal sdt As String, ByVal sdt1 As String, ByVal Dt1 As Date)
        Try



            Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            Dim firstDayLastMonth As DateTime
            Dim lastDayLastMonth As DateTime
            Dim firstDayThisMonth As DateTime
            'Dim sdt As String
            'Dim sdt1 As String
            firstDayLastMonth = thisMonth.AddMonths(-1)
            firstDayThisMonth = thisMonth.AddMonths(0)
            lastDayLastMonth = thisMonth.AddDays(-1)
            'sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
            'sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

            oRecordSet.DoQuery("SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103'   and  T0.DocDate between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T1.U_St,'') <> 'Closed'")
            Dim i As Integer = 0
            Dim J As Integer = 0
            'Dim oJV As SAPbobsCOM.JournalVouchers
            'oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)
            'SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',
            'T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103'   and  T0.DocDate  between '" & sdt & "' and '" & sdt1 & "'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    Dim SPDog As String = ""
                    SPDog = SpecialDogs(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany)
                    If SPDog <> "Yes" Then
                        For J = 1 To oRecordSet.Fields.Item("Quantity").Value
                            Dim oGeneralService As SAPbobsCOM.GeneralService
                            Dim oGeneralData As SAPbobsCOM.GeneralData
                            Dim oSons As SAPbobsCOM.GeneralDataCollection
                            Dim oSon As SAPbobsCOM.GeneralData
                            Dim sCmp As SAPbobsCOM.CompanyService
                            sCmp = PublicVariable.oCompany.GetCompanyService
                            oGeneralService = sCmp.GetGeneralService("INVOICEOB")
                            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                            oGeneralData.SetProperty("U_Type", "Invoice")
                            oGeneralData.SetProperty("U_SubType", "Boarding")
                            oGeneralData.SetProperty("U_InvDate", oRecordSet.Fields.Item("DocDate").Value)
                            oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                            oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                            oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                            oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                            oGeneralData.SetProperty("U_Count", 1)
                            oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_InvNo", oRecordSet.Fields.Item("DocEntry").Value)
                            oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value)
                            oGeneralData.SetProperty("U_UnitPrice", oRecordSet.Fields.Item("PriceBefDi").Value)
                            oGeneralService.Add(oGeneralData)
                        Next
                    End If
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE INV1 SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & oRecordSet.Fields.Item("LineNum").Value & "'")
                oRecordSet.MoveNext()
            Next
            oRecordSet.DoQuery("SELECT T0.[U_Date],T0.[Code], T0.[Name], T0.[U_Petcode], T0.[U_PeName], T0.[U_CardCode], T0.[U_BORBAL] FROM [dbo].[@OPENINGBALANCE]  T0 WHERE T0.[U_BORBAL] > 0 and  T0.[U_Date]  between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T0.U_St1,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                '------------------------
                Try
                    Dim SPDog As String = ""
                    SPDog = SpecialDogs(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany)
                    If SPDog <> "Yes" Then
                        For J = 1 To oRecordSet.Fields.Item("U_BORBAL").Value
                            Dim oGeneralService As SAPbobsCOM.GeneralService
                            Dim oGeneralData As SAPbobsCOM.GeneralData
                            Dim oSons As SAPbobsCOM.GeneralDataCollection
                            Dim oSon As SAPbobsCOM.GeneralData
                            Dim sCmp As SAPbobsCOM.CompanyService
                            sCmp = PublicVariable.oCompany.GetCompanyService
                            oGeneralService = sCmp.GetGeneralService("INVOICEOB")
                            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                            oGeneralData.SetProperty("U_Type", "OB")
                            oGeneralData.SetProperty("U_SubType", "Boarding")
                            oGeneralData.SetProperty("U_InvDate", oRecordSet.Fields.Item("U_Date").Value)
                            oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_Petcode").Value)
                            oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_PeName").Value)
                            oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("U_CardCode").Value)
                            oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_Count", 1)
                            oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                            oGeneralData.SetProperty("U_InvNo", oRecordSet.Fields.Item("Code").Value)
                            'oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("Code").Value)
                            'oGeneralData.SetProperty("U_UnitPrice", oRecordSet.Fields.Item("PriceBefDi").Value)
                            oGeneralService.Add(oGeneralData)
                        Next

                    End If
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE [@OPENINGBALANCE] SET U_ST1='Closed' where Code='" & oRecordSet.Fields.Item("Code").Value & "'")
                oRecordSet.MoveNext()
            Next
            ''MsgBox("Finish")
            '-----------------End Transport--------

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub JV_Tally(ByVal sdt As String, ByVal sdt1 As String, ByVal Dt1 As Date)
        Try
            Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            Dim firstDayLastMonth As DateTime
            Dim lastDayLastMonth As DateTime
            Dim firstDayThisMonth As DateTime
            'Dim sdt As String
            'Dim sdt1 As String
            firstDayLastMonth = thisMonth.AddMonths(-1)
            firstDayThisMonth = thisMonth.AddMonths(0)
            lastDayLastMonth = thisMonth.AddDays(-1)
            'sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
            'sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            oRecordSet4 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[DocEntry], T0.[U_Type], T0.[U_SubType], T0.[U_AttDate], T0.[U_PetCode], T0.[U_PetName], T0.[U_CustCode], T0.[U_CustName], T0.[U_PetSize],T0.U_AttNo FROM [dbo].[@ATTENDANCE]  T0 WHERE isnull( T0.[U_St] ,'') <> 'Closed' and T0.U_SubType='Boarding'")
            Dim i As Integer = 0
            Dim J As Integer = 0
            Dim CustCode As String = ""
            Dim AttDocEntry As String = ""
            Dim AttType As String = ""
            Dim SubType As String = ""
            Dim AttNo As String = ""
            Dim Attdate As String = ""
            Dim InvDocEntry As String = ""
            'Dim oJV As SAPbobsCOM.JournalVouchers
            'oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)
            'SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',
            'T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103'   and  T0.DocDate  between '" & sdt & "' and '" & sdt1 & "'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    CustCode = oRecordSet.Fields.Item("U_CustCode").Value
                    AttDocEntry = oRecordSet.Fields.Item("DocEntry").Value.ToString
                    AttType = oRecordSet.Fields.Item("U_Type").Value
                    SubType = oRecordSet.Fields.Item("U_SubType").Value
                    AttNo = oRecordSet.Fields.Item("U_AttNo").Value
                    Attdate = Format(oRecordSet.Fields.Item("U_AttDate").Value, "yyyyMMdd")
                    oRecordSet1.DoQuery("SELECT T0.[DocEntry] FROM [dbo].[@INVOICEOB]  T0 WHERE T0.[U_CustCode]='" & CustCode & "' and [U_SubType]='" & SubType & "' and isnull( T0.[U_AttNo]  ,'') = ''")
                    If oRecordSet1.RecordCount > 0 Then
                        InvDocEntry = oRecordSet1.Fields.Item("DocEntry").Value
                        If AttType = "CREDITMEMO" Then
                            oRecordSet2.DoQuery("UPDATE [@INVOICEOB] SET U_RefNo='" & AttDocEntry & "',U_AttType='" & AttType & "',U_AttSubType='" & SubType & "',U_AttNo='" & AttNo & "',U_AttDate='" & Attdate & "',U_ConsDate='" & Attdate & "' where DocEntry='" & InvDocEntry & "'")
                        Else
                            oRecordSet2.DoQuery("UPDATE [@INVOICEOB] SET U_RefNo='" & AttDocEntry & "',U_AttType='" & AttType & "',U_AttSubType='" & SubType & "',U_AttNo='" & AttNo & "',U_AttDate='" & Attdate & "',U_ConsDate='" & Attdate & "',U_RegDate='" & Attdate & "' where DocEntry='" & InvDocEntry & "'")
                        End If

                        oRecordSet2.DoQuery("UPDATE [@ATTENDANCE] SET U_St='Closed' where DocEntry='" & AttDocEntry & "'")
                    End If
                Catch ex As Exception
                End Try
                oRecordSet.MoveNext()
            Next

            oRecordSet3.DoQuery("SELECT sum(T0.[U_UnitPrice]) 'UP' FROM [dbo].[@INVOICEOB]  T0 WHERE isnull(T0.[U_St],'') <> 'Closed' and T0.[U_SubType] ='Boarding' and  T0.[U_RegDate]  between '" & Cdt & "' and '" & sdt1 & "'")

            Dim oJV As SAPbobsCOM.JournalVouchers
            oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)

            oJV.JournalEntries.TaxDate = Now.Date.AddDays(0)
            oJV.JournalEntries.VatDate = Now.Date.AddDays(0)
            Try
                oJV.JournalEntries.Lines.AccountCode = "4-2812"
                oJV.JournalEntries.Lines.Credit = (oRecordSet3.Fields.Item(0).Value)
                oJV.JournalEntries.Lines.Add()
            Catch ex As Exception
            End Try
            Try
                oJV.JournalEntries.Lines.AccountCode = "2-7100"
                oJV.JournalEntries.Lines.Debit = (oRecordSet3.Fields.Item(0).Value)
                oJV.JournalEntries.Lines.Add()
            Catch ex As Exception
            End Try
            oJV.JournalEntries.Memo = "BOARDING:" & sdt1
            oJV.JournalEntries.Add()
            Dim K As Integer = 0
            If oRecordSet3.Fields.Item(0).Value <> 0 Then
                K = oJV.Add()
                Dim st As String
                PublicVariable.oCompany.GetLastError(K, st)
                If K <> 0 Then
                    'MsgBox(st)
                Else
                    oRecordSet3.DoQuery("update [dbo].[@INVOICEOB]  set  [U_St]='Closed' WHERE [U_SubType] ='Boarding' and  [U_RegDate]  between '" & Cdt & "' and '" & sdt1 & "'")
                End If
            End If

            '''MsgBox("Finish")
            '-----------------End Transport--------

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub JV_Tally_DAYPASS(ByVal sdt As String, ByVal sdt1 As String, ByVal Dt1 As Date)
        Try
            Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            Dim firstDayLastMonth As DateTime
            Dim lastDayLastMonth As DateTime
            Dim firstDayThisMonth As DateTime
            'Dim sdt As String
            'Dim sdt1 As String
            firstDayLastMonth = thisMonth.AddMonths(-1)
            firstDayThisMonth = thisMonth.AddMonths(0)
            lastDayLastMonth = thisMonth.AddDays(-1)
            'sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
            'sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            oRecordSet4 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[DocEntry], T0.[U_Type], T0.[U_SubType], T0.[U_AttDate], T0.[U_PetCode], T0.[U_PetName], T0.[U_CustCode], T0.[U_CustName], T0.[U_PetSize],T0.U_AttNo FROM [dbo].[@ATTENDANCE]  T0 WHERE isnull( T0.[U_St] ,'') <> 'Closed' and T0.U_SubType='DAYPASS'")
            Dim i As Integer = 0
            Dim J As Integer = 0
            Dim CustCode As String = ""
            Dim AttDocEntry As String = ""
            Dim AttType As String = ""
            Dim SubType As String = ""
            Dim AttNo As String = ""
            Dim Attdate As String = ""
            Dim InvDocEntry As String = ""
            Dim PetSize As String = ""
            'Dim oJV As SAPbobsCOM.JournalVouchers
            'oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)
            'SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',
            'T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103'   and  T0.DocDate  between '" & sdt & "' and '" & sdt1 & "'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    CustCode = oRecordSet.Fields.Item("U_CustCode").Value
                    AttDocEntry = oRecordSet.Fields.Item("DocEntry").Value.ToString
                    AttType = oRecordSet.Fields.Item("U_Type").Value
                    SubType = oRecordSet.Fields.Item("U_SubType").Value
                    AttNo = oRecordSet.Fields.Item("U_AttNo").Value
                    PetSize = oRecordSet.Fields.Item("U_PetSize").Value
                    Attdate = Format(oRecordSet.Fields.Item("U_AttDate").Value, "yyyyMMdd")
                    oRecordSet1.DoQuery("SELECT T0.[DocEntry] FROM [dbo].[@INVOICEOB]  T0 WHERE T0.[U_CustCode]='" & CustCode & "' and [U_SubType]='" & SubType & "' and  T0.[U_PetSize]='" & PetSize & "' and isnull( T0.[U_AttNo]  ,'') = ''")
                    If oRecordSet1.RecordCount > 0 Then
                        InvDocEntry = oRecordSet1.Fields.Item("DocEntry").Value
                        If AttType = "CREDITMEMO" Then
                            oRecordSet2.DoQuery("UPDATE [@INVOICEOB] SET U_RefNo='" & AttDocEntry & "',U_AttType='" & AttType & "',U_AttSubType='" & SubType & "',U_AttNo='" & AttNo & "',U_AttDate='" & Attdate & "',U_ConsDate='" & Attdate & "' where DocEntry='" & InvDocEntry & "'")
                        Else
                            oRecordSet2.DoQuery("UPDATE [@INVOICEOB] SET U_RefNo='" & AttDocEntry & "',U_AttType='" & AttType & "',U_AttSubType='" & SubType & "',U_AttNo='" & AttNo & "',U_AttDate='" & Attdate & "',U_ConsDate='" & Attdate & "',U_RegDate='" & Attdate & "' where DocEntry='" & InvDocEntry & "'")
                        End If

                        oRecordSet2.DoQuery("UPDATE [@ATTENDANCE] SET U_St='Closed' where DocEntry='" & AttDocEntry & "'")
                    End If
                Catch ex As Exception
                End Try
                oRecordSet.MoveNext()
            Next

            oRecordSet3.DoQuery("SELECT sum(T0.[U_UnitPrice]) 'UP' FROM [dbo].[@INVOICEOB]  T0 WHERE isnull(T0.[U_St],'') <> 'Closed' and T0.[U_SubType] ='DAYPASS' and  T0.[U_RegDate]  between '" & Cdt & "' and '" & sdt1 & "'")

            Dim oJV As SAPbobsCOM.JournalVouchers
            oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)

            oJV.JournalEntries.TaxDate = Now.Date.AddDays(0)
            oJV.JournalEntries.VatDate = Now.Date.AddDays(0)
            Try
                oJV.JournalEntries.Lines.AccountCode = "4-2810"
                oJV.JournalEntries.Lines.Credit = (oRecordSet3.Fields.Item(0).Value)
                oJV.JournalEntries.Lines.Add()
            Catch ex As Exception
            End Try
            Try
                oJV.JournalEntries.Lines.AccountCode = "2-7100"
                oJV.JournalEntries.Lines.Debit = (oRecordSet3.Fields.Item(0).Value)
                oJV.JournalEntries.Lines.Add()
            Catch ex As Exception
            End Try
            oJV.JournalEntries.Memo = "DAYPASS:" & sdt1
            oJV.JournalEntries.Add()
            Dim K As Integer = 0
            If oRecordSet3.Fields.Item(0).Value <> 0 Then
                K = oJV.Add()
                Dim st As String
                PublicVariable.oCompany.GetLastError(K, st)
                If K <> 0 Then
                    'MsgBox(st)
                Else
                    oRecordSet3.DoQuery("update [dbo].[@INVOICEOB]  set  [U_St]='Closed' WHERE [U_SubType] ='DAYPASS' and  [U_RegDate]  between '" & Cdt & "' and '" & sdt1 & "'")
                End If
            End If

            '''MsgBox("Finish")
            '-----------------End Transport--------

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub JV_Tally_TRANSPORT(ByVal sdt As String, ByVal sdt1 As String, ByVal Dt1 As Date)
        Try
            Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            Dim firstDayLastMonth As DateTime
            Dim lastDayLastMonth As DateTime
            Dim firstDayThisMonth As DateTime
            'Dim sdt As String
            'Dim sdt1 As String
            firstDayLastMonth = thisMonth.AddMonths(-1)
            firstDayThisMonth = thisMonth.AddMonths(0)
            lastDayLastMonth = thisMonth.AddDays(-1)
            'sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
            'sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            oRecordSet4 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[DocEntry], T0.[U_Type], T0.[U_SubType], T0.[U_AttDate], T0.[U_PetCode], T0.[U_PetName], T0.[U_CustCode], T0.[U_CustName], T0.[U_PetSize],T0.U_AttNo FROM [dbo].[@ATTENDANCE]  T0 WHERE isnull( T0.[U_St] ,'') <> 'Closed' and T0.U_SubType='TRANSPORT'")
            Dim i As Integer = 0
            Dim J As Integer = 0
            Dim CustCode As String = ""
            Dim AttDocEntry As String = ""
            Dim AttType As String = ""
            Dim SubType As String = ""
            Dim AttNo As String = ""
            Dim Attdate As String = ""
            Dim InvDocEntry As String = ""
            'Dim oJV As SAPbobsCOM.JournalVouchers
            'oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)
            'SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',
            'T1.U_AI_PetCode,T1.U_AI_PetName FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103'   and  T0.DocDate  between '" & sdt & "' and '" & sdt1 & "'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    CustCode = oRecordSet.Fields.Item("U_CustCode").Value
                    AttDocEntry = oRecordSet.Fields.Item("DocEntry").Value.ToString
                    AttType = oRecordSet.Fields.Item("U_Type").Value
                    SubType = oRecordSet.Fields.Item("U_SubType").Value
                    AttNo = oRecordSet.Fields.Item("U_AttNo").Value
                    Attdate = Format(oRecordSet.Fields.Item("U_AttDate").Value, "yyyyMMdd")
                    oRecordSet1.DoQuery("SELECT T0.[DocEntry] FROM [dbo].[@INVOICEOB]  T0 WHERE T0.[U_CustCode]='" & CustCode & "' and [U_SubType]='" & SubType & "' and isnull( T0.[U_AttNo]  ,'') = ''")
                    If oRecordSet1.RecordCount > 0 Then
                        InvDocEntry = oRecordSet1.Fields.Item("DocEntry").Value
                        If AttType = "CREDITMEMO" Then
                            oRecordSet2.DoQuery("UPDATE [@INVOICEOB] SET U_RefNo='" & AttDocEntry & "',U_AttType='" & AttType & "',U_AttSubType='" & SubType & "',U_AttNo='" & AttNo & "',U_AttDate='" & Attdate & "',U_ConsDate='" & Attdate & "' where DocEntry='" & InvDocEntry & "'")
                        Else
                            oRecordSet2.DoQuery("UPDATE [@INVOICEOB] SET U_RefNo='" & AttDocEntry & "',U_AttType='" & AttType & "',U_AttSubType='" & SubType & "',U_AttNo='" & AttNo & "',U_AttDate='" & Attdate & "',U_ConsDate='" & Attdate & "',U_RegDate='" & Attdate & "' where DocEntry='" & InvDocEntry & "'")
                        End If

                        oRecordSet2.DoQuery("UPDATE [@ATTENDANCE] SET U_St='Closed' where DocEntry='" & AttDocEntry & "'")
                    End If
                Catch ex As Exception
                End Try
                oRecordSet.MoveNext()
            Next
            oRecordSet3.DoQuery("SELECT sum(T0.[U_UnitPrice]) 'UP' FROM [dbo].[@INVOICEOB]  T0 WHERE isnull(T0.[U_St],'') <> 'Closed' and T0.[U_SubType] ='TRANSPORT' and  T0.[U_RegDate]  between '" & Cdt & "' and '" & sdt1 & "'")

            Dim oJV As SAPbobsCOM.JournalVouchers
            oJV = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)

            oJV.JournalEntries.TaxDate = Now.Date.AddDays(0)
            oJV.JournalEntries.VatDate = Now.Date.AddDays(0)
            Try
                oJV.JournalEntries.Lines.AccountCode = "4-2814"
                oJV.JournalEntries.Lines.Credit = (oRecordSet3.Fields.Item(0).Value)
                oJV.JournalEntries.Lines.Add()
            Catch ex As Exception
            End Try
            Try
                oJV.JournalEntries.Lines.AccountCode = "2-7100"
                oJV.JournalEntries.Lines.Debit = (oRecordSet3.Fields.Item(0).Value)
                oJV.JournalEntries.Lines.Add()
            Catch ex As Exception
            End Try
            oJV.JournalEntries.Memo = "TRANSPORT:" & sdt1
            oJV.JournalEntries.Add()
            Dim K As Integer = 0
            K = oJV.Add()
            Dim st As String
            PublicVariable.oCompany.GetLastError(K, st)
            If K <> 0 Then
                'MsgBox(st)
            Else
                oRecordSet3.DoQuery("update [dbo].[@INVOICEOB]  set  [U_St]='Closed' WHERE [U_SubType] ='TRANSPORT' and  [U_RegDate]  between '" & Cdt & "' and '" & sdt1 & "'")
            End If

            '''MsgBox("Finish")
            '-----------------End Transport--------

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub JV_ATTENDANCE_DAYPASS(ByVal sdt As String, ByVal sdt1 As String, ByVal Dt1 As Date)
        Try
            Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            Dim firstDayLastMonth As DateTime
            Dim lastDayLastMonth As DateTime
            Dim firstDayThisMonth As DateTime
            'Dim sdt As String
            'Dim sdt1 As String
            firstDayLastMonth = thisMonth.AddMonths(-1)
            firstDayThisMonth = thisMonth.AddMonths(0)
            lastDayLastMonth = thisMonth.AddDays(-1)
            'sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
            'sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            oRecordSet4 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim i As Integer = 0
            Dim J As Integer = 0
            oRecordSet.DoQuery("SELECT T1.[Quantity],isnull(T1.U_FreeDP,0) U_FreeDP,T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',T1.U_AI_PetCode,T1.U_AI_PetName FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104'   and  T0.DocDate between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T1.U_St,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    For J = 1 To oRecordSet.Fields.Item("Quantity").Value
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "CREDITMEMO")
                        oGeneralData.SetProperty("U_SubType", "DAYPASS")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("DocDate").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                        oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                        oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("DocEntry").Value.ToString)
                        oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value.ToString)
                        oGeneralService.Add(oGeneralData)
                    Next
                    For J = 1 To oRecordSet.Fields.Item("U_FreeDP").Value
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "CREDITMEMO")
                        oGeneralData.SetProperty("U_SubType", "DAYPASS")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("DocDate").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                        oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                        oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("DocEntry").Value.ToString)
                        oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value.ToString)
                        oGeneralService.Add(oGeneralData)
                    Next
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE RIN1 SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & oRecordSet.Fields.Item("LineNum").Value & "'")
                oRecordSet.MoveNext()
            Next

            ''For i = 1 To oRecordSet.RecordCount
            ''    Try
            ''        For J = 1 To oRecordSet.Fields.Item("U_FreeDP").Value
            ''            Dim oGeneralService As SAPbobsCOM.GeneralService
            ''            Dim oGeneralData As SAPbobsCOM.GeneralData
            ''            Dim oSons As SAPbobsCOM.GeneralDataCollection
            ''            Dim oSon As SAPbobsCOM.GeneralData
            ''            Dim sCmp As SAPbobsCOM.CompanyService
            ''            sCmp = PublicVariable.oCompany.GetCompanyService
            ''            oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
            ''            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
            ''            oGeneralData.SetProperty("U_Type", "CREDITMEMO")
            ''            oGeneralData.SetProperty("U_SubType", "DAYPASS")
            ''            oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("DocDate").Value)
            ''            oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
            ''            oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
            ''            oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
            ''            oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
            ''            oGeneralData.SetProperty("U_Count", 1)
            ''            oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
            ''            oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("DocEntry").Value.ToString)
            ''            oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value.ToString)
            ''            oGeneralService.Add(oGeneralData)
            ''        Next
            ''    Catch ex As Exception
            ''    End Try
            ''    oRecordSet2.DoQuery("UPDATE INV1 SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & oRecordSet.Fields.Item("LineNum").Value & "'")
            ''    oRecordSet.MoveNext()
            ''Next
            '---------TRANSPORT Attendance-----------------------
            oRecordSet.DoQuery("SELECT T0.[DocEntry], T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_Date] FROM [dbo].[@AI_ATTENDANCE]  T0 WHERE isnull( T0.[U_St2] ,'') <> 'Closed'  and  T0.[U_AI_Date]  between '" & Cdt & "' and '" & sdt1 & "' and isnull(T0.U_Ignore,'') <> 'Y'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    Dim oGeneralService As SAPbobsCOM.GeneralService
                    Dim oGeneralData As SAPbobsCOM.GeneralData
                    Dim oSons As SAPbobsCOM.GeneralDataCollection
                    Dim oSon As SAPbobsCOM.GeneralData
                    Dim sCmp As SAPbobsCOM.CompanyService
                    sCmp = PublicVariable.oCompany.GetCompanyService
                    oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                    oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                    oGeneralData.SetProperty("U_Type", "ATTENDANCE")
                    oGeneralData.SetProperty("U_SubType", "DAYPASS")
                    oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("U_AI_Date").Value)
                    oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                    oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                    oGeneralData.SetProperty("U_CustCode", CardCode(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                    oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                    oGeneralData.SetProperty("U_Count", 1)
                    oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                    oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("DocEntry").Value.ToString)
                    oGeneralService.Add(oGeneralData)

                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE [@AI_ATTENDANCE] SET U_ST2='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "'")
                oRecordSet.MoveNext()
            Next



            Dim Qty As Integer = 0
            oRecordSet.DoQuery("SELECT T0.[U_Date],T0.[Code], T0.[Name], T0.[U_Petcode], T0.[U_PeName], T0.[U_CardCode], T0.[U_DP] FROM [dbo].[@OPENINGBALANCE]  T0 WHERE T0.[U_DP] < 0 and  T0.[U_Date]  between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T0.U_St2,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                '------------------------
                Try
                    Qty = oRecordSet.Fields.Item("U_DP").Value
                    If Qty < 0 Then
                        Qty = Qty * -1
                    End If
                    For J = 1 To Qty
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "OB")
                        oGeneralData.SetProperty("U_SubType", "DAYPASS")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("U_Date").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_Petcode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_PeName").Value)
                        oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("U_CardCode").Value)
                        oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("Code").Value)
                        oGeneralService.Add(oGeneralData)
                    Next
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE [@OPENINGBALANCE] SET U_ST2='Closed' where Code='" & oRecordSet.Fields.Item("Code").Value & "'")
                oRecordSet.MoveNext()
            Next
            '''MsgBox("Finish")
            '-----------------End Transport--------

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub JV_ATTENDANCE_TRANSPORT(ByVal sdt As String, ByVal sdt1 As String, ByVal Dt1 As Date)
        Try
            Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            Dim firstDayLastMonth As DateTime
            Dim lastDayLastMonth As DateTime
            Dim firstDayThisMonth As DateTime
            'Dim sdt As String
            'Dim sdt1 As String
            firstDayLastMonth = thisMonth.AddMonths(-1)
            firstDayThisMonth = thisMonth.AddMonths(0)
            lastDayLastMonth = thisMonth.AddDays(-1)
            'sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
            'sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            oRecordSet4 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim i As Integer = 0
            Dim J As Integer = 0
            '--------one way
            oRecordSet.DoQuery("SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',T1.U_AI_PetCode,T1.U_AI_PetName FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='110'   and  T0.DocDate between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T1.U_St,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    For J = 1 To oRecordSet.Fields.Item("Quantity").Value
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "CREDITMEMO")
                        oGeneralData.SetProperty("U_SubType", "TRANSPORT")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("DocDate").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                        oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                        oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("DocEntry").Value.ToString)
                        oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value.ToString)
                        oGeneralService.Add(oGeneralData)
                    Next
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE RIN1 SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & oRecordSet.Fields.Item("LineNum").Value & "'")
                oRecordSet.MoveNext()
            Next
            '----tow way
            oRecordSet.DoQuery("SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',T1.U_AI_PetCode,T1.U_AI_PetName FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='109'   and  T0.DocDate between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T1.U_St,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    For J = 1 To (oRecordSet.Fields.Item("Quantity").Value * 2)
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "CREDITMEMO")
                        oGeneralData.SetProperty("U_SubType", "TRANSPORT")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("DocDate").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                        oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                        oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("DocEntry").Value.ToString)
                        oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value.ToString)
                        oGeneralService.Add(oGeneralData)
                    Next
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE RIN1 SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & oRecordSet.Fields.Item("LineNum").Value & "'")
                oRecordSet.MoveNext()
            Next
            '---------TRANSPORT Attendance-----------------------
            '-----Pick Up---------
            oRecordSet.DoQuery("SELECT T0.[DocEntry], T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_Date] FROM [dbo].[@AI_ATTENDANCE]  T0 WHERE isnull( T0.[U_St] ,'') <> 'Closed'  and  T0.[U_AI_Date]  between '" & Cdt & "' and '" & sdt1 & "' and  isnull(T0.[U_AI_Pickup] ,'') ='Y'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    Dim CustmerCode As String = CardCode(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany)
                    Dim dt As String = Format(oRecordSet.Fields.Item("U_AI_Date").Value, "yyyyMMdd")
                    oRecordSet3.DoQuery("SELECT distinct T1.[U_AI_CustCode],T0.[U_AI_PetCode] FROM [dbo].[@AI_ATTENDANCE]  T0 left join[dbo].[@AI_PETMASTER]  T1 on  T1.[U_PetCode] = T0.[U_AI_PetCode] WHERE T1.[U_AI_CustCode] ='" & CustmerCode & "' and  T0.[U_AI_Date] ='" & dt & "' and T0.[U_AI_Pickup]='Y'")
                    If oRecordSet3.RecordCount <= 1 Then
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "ATTENDANCE")
                        oGeneralData.SetProperty("U_SubType", "TRANSPORT")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("U_AI_Date").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                        oGeneralData.SetProperty("U_CustCode", CardCode(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("DocEntry").Value.ToString)
                        oGeneralService.Add(oGeneralData)
                    End If
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE [@AI_ATTENDANCE] SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "'")
                oRecordSet.MoveNext()
            Next

            '-----Drop off---------
            oRecordSet.DoQuery("SELECT T0.[DocEntry], T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_Date] FROM [dbo].[@AI_ATTENDANCE]  T0 WHERE isnull( T0.[U_St1] ,'') <> 'Closed'  and  T0.[U_AI_Date]  between '" & Cdt & "' and '" & sdt1 & "' and  isnull(T0.[U_AI_Dropoff] ,'') ='Y'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    Dim CustmerCode As String = CardCode(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany)
                    Dim dt As String = Format(oRecordSet.Fields.Item("U_AI_Date").Value, "yyyyMMdd")
                    oRecordSet3.DoQuery("SELECT distinct T1.[U_AI_CustCode],T0.[U_AI_PetCode] FROM [dbo].[@AI_ATTENDANCE]  T0 left join[dbo].[@AI_PETMASTER]  T1 on  T1.[U_PetCode] = T0.[U_AI_PetCode] WHERE T1.[U_AI_CustCode] ='" & CustmerCode & "' and  T0.[U_AI_Date] ='" & dt & "' and T0.[U_AI_Dropoff]='Y'")
                    If oRecordSet3.RecordCount <= 1 Then
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "ATTENDANCE")
                        oGeneralData.SetProperty("U_SubType", "TRANSPORT")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("U_AI_Date").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                        oGeneralData.SetProperty("U_CustCode", CardCode(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("DocEntry").Value.ToString)
                        oGeneralService.Add(oGeneralData)
                    End If
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE [@AI_ATTENDANCE] SET U_ST1='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "'")
                oRecordSet.MoveNext()
            Next

            Dim Qty As Integer = 0
            oRecordSet.DoQuery("SELECT T0.[U_Date],T0.[Code], T0.[Name], T0.[U_Petcode], T0.[U_PeName], T0.[U_CardCode], T0.[U_TRNSBAL] FROM [dbo].[@OPENINGBALANCE]  T0 WHERE T0.[U_TRNSBAL] < 0 and  T0.[U_Date]  between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T0.U_St,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                '------------------------
                Try
                    Qty = oRecordSet.Fields.Item("U_TRNSBAL").Value
                    If Qty < 0 Then
                        Qty = Qty * -1
                    End If
                    For J = 1 To Qty
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "OB")
                        oGeneralData.SetProperty("U_SubType", "TRANSPORT")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("U_Date").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_Petcode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_PeName").Value)
                        oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("U_CardCode").Value)
                        oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("Code").Value)
                        oGeneralService.Add(oGeneralData)
                    Next
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE [@OPENINGBALANCE] SET U_ST='Closed' where Code='" & oRecordSet.Fields.Item("Code").Value & "'")
                oRecordSet.MoveNext()
            Next

            '''MsgBox("Finish")
            '-----------------End Transport--------

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub JV_ATTENDANCE(ByVal sdt As String, ByVal sdt1 As String, ByVal Dt1 As Date)
        Try
            Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            Dim firstDayLastMonth As DateTime
            Dim lastDayLastMonth As DateTime
            Dim firstDayThisMonth As DateTime
            'Dim sdt As String
            'Dim sdt1 As String
            firstDayLastMonth = thisMonth.AddMonths(-1)
            firstDayThisMonth = thisMonth.AddMonths(0)
            lastDayLastMonth = thisMonth.AddDays(-1)
            'sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
            'sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            oRecordSet4 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim i As Integer = 0
            Dim J As Integer = 0

            oRecordSet.DoQuery("SELECT T1.[Quantity],T1.[PriceBefDi],T0.DocEntry,T1.LineNum,T0.DocDate,T0.[CardCode],T0.[CardName],(T1.[Quantity]) as 'Qty',T1.U_AI_PetCode,T1.U_AI_PetName FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103'   and  T0.DocDate between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T1.U_St,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    For J = 1 To oRecordSet.Fields.Item("Quantity").Value
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "CREDITMEMO")
                        oGeneralData.SetProperty("U_SubType", "Boarding")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("DocDate").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                        oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("CardCode").Value)
                        oGeneralData.SetProperty("U_CustName", oRecordSet.Fields.Item("CardName").Value)
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("DocEntry").Value.ToString)
                        oGeneralData.SetProperty("U_LineNo", oRecordSet.Fields.Item("LineNum").Value.ToString)
                        oGeneralService.Add(oGeneralData)
                    Next
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE RIN1 SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & oRecordSet.Fields.Item("LineNum").Value & "'")
                oRecordSet.MoveNext()
            Next
            '---------Boarding Attendance-----------------------
            oRecordSet.DoQuery("SELECT T0.[DocEntry], T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_ChkDt] FROM [dbo].[@AI_NIGHTSHIFT]  T0 left  join [dbo].[@AI_PETMASTER]  T1 on T1.U_PetCode=T0.[U_AI_PetCode] WHERE T1.[U_AI_Boarding] <>  'Permanent' and isnull( T0.[U_St] ,'') <> 'Closed' and  T0.[U_AI_ChkDt] between '" & Cdt & "' and '" & sdt1 & "' and isnull(T0.U_Ignore,'') <> 'Y'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    Dim oGeneralService As SAPbobsCOM.GeneralService
                    Dim oGeneralData As SAPbobsCOM.GeneralData
                    Dim oSons As SAPbobsCOM.GeneralDataCollection
                    Dim oSon As SAPbobsCOM.GeneralData
                    Dim sCmp As SAPbobsCOM.CompanyService
                    sCmp = PublicVariable.oCompany.GetCompanyService
                    oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                    oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                    oGeneralData.SetProperty("U_Type", "ATTENDANCE")
                    oGeneralData.SetProperty("U_SubType", "Boarding")
                    oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("U_AI_ChkDt").Value)
                    oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_AI_PetCode").Value)
                    oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_AI_PetName").Value)
                    oGeneralData.SetProperty("U_CustCode", CardCode(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                    oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                    oGeneralData.SetProperty("U_Count", 1)
                    oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_AI_PetCode").Value, PublicVariable.oCompany))
                    J = oRecordSet.Fields.Item("DocEntry").Value
                    oGeneralData.SetProperty("U_AttNo", J.ToString)
                    oGeneralService.Add(oGeneralData)
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE [@AI_NIGHTSHIFT] SET U_ST='Closed' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "'")
                oRecordSet.MoveNext()
            Next

            Dim Qty As Integer = 0
            oRecordSet.DoQuery("SELECT T0.[U_Date],T0.[Code], T0.[Name], T0.[U_Petcode], T0.[U_PeName], T0.[U_CardCode], T0.[U_BORBAL] FROM [dbo].[@OPENINGBALANCE]  T0 WHERE T0.[U_BORBAL] < 0 and  T0.[U_Date]  between '" & Cdt & "' and  '" & sdt1 & "' and isnull(T0.U_St1,'') <> 'Closed'")
            For i = 1 To oRecordSet.RecordCount
                '------------------------
                Try
                    Qty = oRecordSet.Fields.Item("U_BORBAL").Value
                    If Qty < 0 Then
                        Qty = Qty * -1
                    End If
                    For J = 1 To Qty
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = PublicVariable.oCompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("ATTENDANCE")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_Type", "OB")
                        oGeneralData.SetProperty("U_SubType", "Boarding")
                        oGeneralData.SetProperty("U_AttDate", oRecordSet.Fields.Item("U_Date").Value)
                        oGeneralData.SetProperty("U_PetCode", oRecordSet.Fields.Item("U_Petcode").Value)
                        oGeneralData.SetProperty("U_PetName", oRecordSet.Fields.Item("U_PeName").Value)
                        oGeneralData.SetProperty("U_CustCode", oRecordSet.Fields.Item("U_CardCode").Value)
                        oGeneralData.SetProperty("U_CustName", CardName(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_Count", 1)
                        oGeneralData.SetProperty("U_PetSize", PSize(oRecordSet.Fields.Item("U_Petcode").Value, PublicVariable.oCompany))
                        oGeneralData.SetProperty("U_AttNo", oRecordSet.Fields.Item("Code").Value)
                        oGeneralService.Add(oGeneralData)
                    Next
                Catch ex As Exception
                End Try
                oRecordSet2.DoQuery("UPDATE [@OPENINGBALANCE] SET U_ST1='Closed' where Code='" & oRecordSet.Fields.Item("Code").Value & "'")
                oRecordSet.MoveNext()
            Next
            '''MsgBox("Finish")
            '-----------------End Transport--------

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Public Function PSize(ByVal PetCode As String, ByVal Ocompany As SAPbobsCOM.Company) As String
        Dim x As String = ""
        Dim oRecordSet_PZ As SAPbobsCOM.Recordset
        oRecordSet_PZ = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        Dim Str As String = "SELECT T0.[U_AI_Size] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'"
        oRecordSet_PZ.DoQuery("SELECT T0.[U_AI_Size] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'")
        x = oRecordSet_PZ.Fields.Item(0).Value
        Return x
    End Function
    Public Function CardCode(ByVal PetCode As String, ByVal Ocompany As SAPbobsCOM.Company) As String
        Dim x As String = ""
        Dim oRecordSet_PZ As SAPbobsCOM.Recordset
        oRecordSet_PZ = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        'Dim Str As String = "SELECT T0.[U_AI_Size] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'"
        oRecordSet_PZ.DoQuery("SELECT T0.[U_AI_CustCode] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'")
        x = oRecordSet_PZ.Fields.Item(0).Value
        Return x
    End Function
    Public Function CardName(ByVal PetCode As String, ByVal Ocompany As SAPbobsCOM.Company) As String
        Dim x As String = ""
        Dim oRecordSet_PZ As SAPbobsCOM.Recordset
        oRecordSet_PZ = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSet_PZ.DoQuery("SELECT T0.[U_AI_CustName] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'")
        x = oRecordSet_PZ.Fields.Item(0).Value
        Return x
    End Function

    Public Function SpecialDogs(ByVal PetCode As String, ByVal Ocompany As SAPbobsCOM.Company) As String
        Dim x As String = ""
        Dim oRecordSet_PZ As SAPbobsCOM.Recordset
        oRecordSet_PZ = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSet_PZ.DoQuery("SELECT isnull(T0.[U_SP],'No') FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'")
        x = oRecordSet_PZ.Fields.Item(0).Value
        Return x
    End Function

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                'Button6.Enabled = False
                JV_INVOICE_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
                JV_INVOICE("20140101", StartDt, "2013-02-28") 'feb
                JV_INVOICE_DAYPASS("20140101", StartDt, "2013-02-28") 'feb
                'Button6.Enabled = True
                'MsgBox("JV  Complected")
            End If
        Else
            'Button6.Enabled = False
            JV_INVOICE_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
            JV_INVOICE("20140101", StartDt, "2013-02-28") 'feb
            JV_INVOICE_DAYPASS("20140101", StartDt, "2013-02-28") 'feb
            'Button6.Enabled = True
            'MsgBox("JV  Complected")
        End If

    End Sub
    Public Sub DayPass_Invoice_loop1()
        'Dim PrimKey As String = "%"
        'If TextBox1.Text <> "" Then
        '    PrimKey = TextBox1.Text
        'End If
        Dim oRecordSetCLop As SAPbobsCOM.Recordset
        oRecordSetCLop = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSetCLop.DoQuery("select DocEntry from OINV")
        Dim M As Integer = 0
        'Dim oRecordSet5 As SAPbobsCOM.Recordset
        'oRecordSet5 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        'oRecordSet5.DoQuery("update ocrd set U_AI_DayPassUsed=0,U_AI_DayPassUsedM=0,U_AI_DayPassUsedB=0 where CardCode like '" & PrimKey & "'")

        oRecordSetCLop.MoveFirst()
        For M = 0 To oRecordSetCLop.RecordCount - 1

            DayPass_Invoice1(oRecordSetCLop.Fields.Item(0).Value.ToString)

            oRecordSetCLop.MoveNext()
        Next
    End Sub
    Public Sub DayPass_Invoice_loop2()
        'Dim PrimKey As String = "%"
        'If TextBox1.Text <> "" Then
        '    PrimKey = TextBox1.Text
        'End If
        Dim oRecordSetCLop As SAPbobsCOM.Recordset
        oRecordSetCLop = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSetCLop.DoQuery("select DocEntry from ORIN")
        Dim M As Integer = 0
        'Dim oRecordSet5 As SAPbobsCOM.Recordset
        'oRecordSet5 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        'oRecordSet5.DoQuery("update ocrd set U_AI_DayPassUsed=0,U_AI_DayPassUsedM=0,U_AI_DayPassUsedB=0 where CardCode like '" & PrimKey & "'")

        oRecordSetCLop.MoveFirst()
        For M = 0 To oRecordSetCLop.RecordCount - 1

            DayPass_Invoice2(oRecordSetCLop.Fields.Item(0).Value.ToString)
            oRecordSetCLop.MoveNext()
        Next
    End Sub
    Public Sub DayPass_Invoice1(ByVal docnum As String)
        Try
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            Dim oRecordSet5 As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

            'oRecordSet51.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[Price],T1.[U_AI_PetCode] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102')")

            Dim PetSize As String = ""
            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[LineNum] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104') and T0.DocEntry ='" & docnum & "'")

            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet6 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet6.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[U_AI_PackageType] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.DocEntry ='" & docnum & "'")
            If oRecordSet.RecordCount = 0 And oRecordSet6.RecordCount = 0 Then
                Exit Sub
            End If


            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim SmallCount As Integer = 0
            Dim MediumCount As Integer = 0
            Dim LargeCount As Integer = 0
            oRecordSet.MoveFirst()
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            '[U_AI_ExpDate]
            Dim Small As Boolean = False
            Dim Medium As Boolean = False
            Dim Large As Boolean = False
            Dim Qty1 As Integer = 0
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value
                PetSize = PSize(oRecordSet.Fields.Item(2).Value, PublicVariable.oCompany)
                Qty1 = Qty1 + oRecordSet.Fields.Item(1).Value
                If PetSize = "Small" Then
                    Small = True
                    SmallCount = SmallCount + 1
                ElseIf PetSize = "Medium" Then
                    Medium = True
                    MediumCount = MediumCount + 1
                ElseIf PetSize = "Large" Then
                    Large = True
                    LargeCount = LargeCount + 1
                End If
                oRecordSet.MoveNext()
            Next
            oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
            Dim j As Integer
            oRecordSet3.MoveFirst()
            Dim Bonus1 As Integer = 0
            Dim Bought1 As Integer = 0
            Dim BonusPoint1 As Integer = 0
            For j = 1 To oRecordSet3.RecordCount
                Bought1 = oRecordSet3.Fields.Item(0).Value
                Bonus1 = oRecordSet3.Fields.Item(1).Value
                If Qty1 >= Bought1 Then
                    BonusPoint1 = Bonus1
                End If
                oRecordSet3.MoveNext()
            Next
            Dim SmallBP As Integer = 0
            Dim MediumBP As Integer = 0
            Dim LargeBP As Integer = 0
            Dim BonusPoint2 As Integer = 0
            BonusPoint2 = BonusPoint1
            For j = 1 To BonusPoint1
                If Small = True Then
                    SmallBP = SmallBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Medium = True Then
                    MediumBP = MediumBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Large = True Then
                    LargeBP = LargeBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
            Next
            Dim LineNum As Integer = 0
            oRecordSet.MoveFirst()
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value
                LineNum = oRecordSet.Fields.Item(3).Value
                PetSize = PSize(oRecordSet.Fields.Item(2).Value, PublicVariable.oCompany)
                If PetSize = "Small" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (SmallBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + (SmallBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused

                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    'If GroupCode = 0 Then
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'Else
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'End If
                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (SmallBP) & " where [DocEntry]=" & docnum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try

                    If SmallBP <> 0 Then
                        SmallBP = 0
                    End If
                ElseIf PetSize = "Medium" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (MediumBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + (MediumBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused

                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'If GroupCode = 0 Then
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'Else
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'End If
                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (MediumBP) & " where [DocEntry]=" & docnum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try

                    If MediumBP <> 0 Then
                        MediumBP = 0
                    End If
                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (LargeBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + (LargeBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused

                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'If GroupCode = 0 Then
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'Else
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'End If
                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (LargeBP) & " where [DocEntry]=" & docnum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try

                    If LargeBP <> 0 Then
                        LargeBP = 0
                    End If
                End If

                oRecordSet.MoveNext()
            Next

            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            oRecordSet6 = Nothing
            GC.Collect()
        Catch ex As Exception
        End Try
    End Sub
    Public Sub DayPass_Invoice2(ByVal docnum As String)
        Try
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            Dim oRecordSet5 As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

            'oRecordSet51.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[Price],T1.[U_AI_PetCode] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102')")

            Dim PetSize As String = ""
            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[LineNum] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104') and T0.DocEntry ='" & docnum & "'")

            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet6 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet6.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[U_AI_PackageType] ORIN OINV T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.DocEntry ='" & docnum & "'")
            If oRecordSet.RecordCount = 0 And oRecordSet6.RecordCount = 0 Then
                Exit Sub
            End If


            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim SmallCount As Integer = 0
            Dim MediumCount As Integer = 0
            Dim LargeCount As Integer = 0
            oRecordSet.MoveFirst()
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            '[U_AI_ExpDate]
            Dim Small As Boolean = False
            Dim Medium As Boolean = False
            Dim Large As Boolean = False
            Dim Qty1 As Integer = 0
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value
                PetSize = PSize(oRecordSet.Fields.Item(2).Value, PublicVariable.oCompany)
                Qty1 = Qty1 + oRecordSet.Fields.Item(1).Value
                If PetSize = "Small" Then
                    Small = True
                    SmallCount = SmallCount + 1
                ElseIf PetSize = "Medium" Then
                    Medium = True
                    MediumCount = MediumCount + 1
                ElseIf PetSize = "Large" Then
                    Large = True
                    LargeCount = LargeCount + 1
                End If
                oRecordSet.MoveNext()
            Next
            oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
            Dim j As Integer
            oRecordSet3.MoveFirst()
            Dim Bonus1 As Integer = 0
            Dim Bought1 As Integer = 0
            Dim BonusPoint1 As Integer = 0
            For j = 1 To oRecordSet3.RecordCount
                Bought1 = oRecordSet3.Fields.Item(0).Value
                Bonus1 = oRecordSet3.Fields.Item(1).Value
                If Qty1 >= Bought1 Then
                    BonusPoint1 = Bonus1
                End If
                oRecordSet3.MoveNext()
            Next
            Dim SmallBP As Integer = 0
            Dim MediumBP As Integer = 0
            Dim LargeBP As Integer = 0
            Dim BonusPoint2 As Integer = 0
            BonusPoint2 = BonusPoint1
            For j = 1 To BonusPoint1
                If Small = True Then
                    SmallBP = SmallBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Medium = True Then
                    MediumBP = MediumBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Large = True Then
                    LargeBP = LargeBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
            Next
            Dim LineNum As Integer = 0
            oRecordSet.MoveFirst()
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value
                LineNum = oRecordSet.Fields.Item(3).Value
                PetSize = PSize(oRecordSet.Fields.Item(2).Value, PublicVariable.oCompany)
                If PetSize = "Small" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (SmallBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + (SmallBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused

                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    'If GroupCode = 0 Then
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'Else
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'End If
                    Try
                        oRecordSet2.DoQuery("UPDATE RIN1 set U_FreeDP=" & (SmallBP) & " where [DocEntry]=" & docnum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try

                    If SmallBP <> 0 Then
                        SmallBP = 0
                    End If

                ElseIf PetSize = "Medium" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (MediumBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + (MediumBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused

                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'If GroupCode = 0 Then
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'Else
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'End If
                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (MediumBP) & " where [DocEntry]=" & docnum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try

                    If MediumBP <> 0 Then
                        MediumBP = 0
                    End If

                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (LargeBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + (LargeBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused

                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'If GroupCode = 0 Then
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'Else
                    '    oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    'End If
                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (LargeBP) & " where [DocEntry]=" & docnum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try

                    If LargeBP <> 0 Then
                        LargeBP = 0
                    End If
                End If

                oRecordSet.MoveNext()
            Next

            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            oRecordSet6 = Nothing
            GC.Collect()
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                'Button6.Enabled = False
                DayPass_Invoice_loop1()
                DayPass_Invoice_loop2()
                JV_ATTENDANCE_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
                JV_ATTENDANCE("20140101", StartDt, "2013-02-28") 'feb
                JV_ATTENDANCE_DAYPASS("20140101", StartDt, "2013-02-28")
                'Button6.Enabled = True
                'MsgBox("JV  Complected")
            End If
        Else
            'Button6.Enabled = False
            DayPass_Invoice_loop1()
            DayPass_Invoice_loop2()
            JV_ATTENDANCE_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
            JV_ATTENDANCE("20140101", StartDt, "2013-02-28") 'feb
            JV_ATTENDANCE_DAYPASS("20140101", StartDt, "2013-02-28")

            'Button6.Enabled = True
            'MsgBox("JV  Complected")
        End If
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                'Button6.Enabled = False
                JV_Tally_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
                JV_Tally("20140101", StartDt, "2013-02-28")
                JV_Tally_DAYPASS("20140101", StartDt, "2013-02-28")
                'Button6.Enabled = True
                'MsgBox("JV  Complected")
            End If
        Else
            'Button6.Enabled = False
            JV_Tally_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
            JV_Tally("20140101", StartDt, "2013-02-28")
            JV_Tally_DAYPASS("20140101", StartDt, "2013-02-28")
            'Button6.Enabled = True
            'MsgBox("JV  Complected")
        End If
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        Try
            If Connection.bConnect = False Then
                connect.setDB()
                If Not connect.connectDB() Then
                    'MsgBox("SAP Connection Failed")
                    Exit Sub
                Else
                    Dim oRecordSet As SAPbobsCOM.Recordset
                    oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet.DoQuery("UPDATE INV1 SET U_ST='Open'")
                    oRecordSet.DoQuery("UPDATE RIN1 SET U_ST='Open'")
                    oRecordSet.DoQuery("UPDATE [@AI_NIGHTSHIFT] SET U_ST='Open'")
                    oRecordSet.DoQuery("UPDATE [@OPENINGBALANCE] SET U_ST='Open',U_ST1='Open',U_ST2='Open'")
                    oRecordSet.DoQuery("delete from [@ATTENDANCE]")
                    oRecordSet.DoQuery("delete from [@INVOICEOB]") 'OBTD
                    oRecordSet.DoQuery("delete from [OBTD]") 'OBTD
                    oRecordSet.DoQuery("UPDATE [@AI_ATTENDANCE] SET U_ST='Open',U_ST1='Open',U_ST2='Open'")
                    'MsgBox("Complected")
                End If
            Else
                Dim oRecordSet As SAPbobsCOM.Recordset
                oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("UPDATE INV1 SET U_ST='Open'")
                oRecordSet.DoQuery("UPDATE RIN1 SET U_ST='Open'")
                oRecordSet.DoQuery("UPDATE [@AI_NIGHTSHIFT] SET U_ST='Open'")
                oRecordSet.DoQuery("UPDATE [@OPENINGBALANCE] SET U_ST='Open',U_ST1='Open',U_ST2='Open'")
                oRecordSet.DoQuery("delete from [@ATTENDANCE]")
                oRecordSet.DoQuery("delete from [@INVOICEOB]")
                oRecordSet.DoQuery("delete from [OBTD]") 'OBTD
                oRecordSet.DoQuery("UPDATE [@AI_ATTENDANCE] SET U_ST='Open',U_ST1='Open',U_ST2='Open'")
                'MsgBox("Complected")
            End If
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Exit Sub
            If Connection.bConnect = False Then
                connect.setDB()
                If Not connect.connectDB() Then
                    MsgBox("SAP Connection Failed")
                    Exit Sub
                Else
                    Button6.Enabled = False
                    Create_APInvoice()
                    Create_PO()
                    '  Button11.Enabled  = True
                    'MsgBox("JV  Complected")
                End If
            Else
                Button6.Enabled = False
                Create_APInvoice()
                Create_PO()
                'MsgBox("JV  Complected")
            End If
            PublicVariable.oCompany.Disconnect()
            PublicVariable.oCompany = Nothing
            GC.Collect()
            Environment.Exit(0)
            Dim MyArr As Array
            Dim sPath As String
            sPath = IO.Directory.GetParent(Application.StartupPath).ToString.Trim()
            Dim file As System.IO.StreamReader = New System.IO.StreamReader(sPath & "\" & "ServerConfiguration.txt", True)
            Dim sCon As String
            sCon = file.ReadLine()
            MyArr = sCon.Split(";")
            Cdt = MyArr(8)
            StartDt = MyArr(9)
        Catch ex As Exception
            Environment.Exit(0)
        End Try

    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                '  Button11.Enabled  = False
                JV_INVOICE_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
                JV_INVOICE("20140101", StartDt, "2013-02-28") 'feb
                JV_INVOICE_DAYPASS("20140101", StartDt, "2013-02-28") 'feb


                DayPass_Invoice_loop1()
                DayPass_Invoice_loop2()
                JV_ATTENDANCE_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
                JV_ATTENDANCE("20140101", StartDt, "2013-02-28") 'feb
                JV_ATTENDANCE_DAYPASS("20140101", StartDt, "2013-02-28")

                JV_Tally("20140101", StartDt, "2013-02-28")
                JV_Tally_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
                JV_Tally_DAYPASS("20140101", StartDt, "2013-02-28")
                '  Button11.Enabled  = True
                'MsgBox("JV  Complected")
            End If
        Else
            '  Button11.Enabled  = False
            JV_INVOICE_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
            JV_INVOICE("20140101", StartDt, "2013-02-28") 'feb
            JV_INVOICE_DAYPASS("20140101", StartDt, "2013-02-28") 'feb

            DayPass_Invoice_loop1()
            DayPass_Invoice_loop2()
            JV_ATTENDANCE_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
            JV_ATTENDANCE("20140101", StartDt, "2013-02-28") 'feb
            JV_ATTENDANCE_DAYPASS("20140101", StartDt, "2013-02-28")

            JV_Tally("20140101", StartDt, "2013-02-28")
            JV_Tally_TRANSPORT("20140101", StartDt, "2013-02-28") 'feb
            JV_Tally_DAYPASS("20140101", StartDt, "2013-02-28")
            '  Button11.Enabled  = True
            'MsgBox("JV  Complected")
        End If
    End Sub

    Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Exit Sub
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        Exit Sub
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                Button6.Enabled = False
                '  PO_Close()
                '  Button11.Enabled  = True
                'MsgBox("JV  Complected")
            End If
        Else
            Button6.Enabled = False
            ' PO_Close()
            'MsgBox("JV  Complected")
        End If
    End Sub

    Private Sub PO_Close()
        Try
            Dim oPOR As SAPbobsCOM.Documents
            oPOR = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseOrders)
            Dim i As Integer = 0
            Dim K As Integer = 0

            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT DocEntry FROM OPOR T0 where T0.DocStatus='O'")
            For i = 1 To oRecordSet.RecordCount

                oPOR.GetByKey(oRecordSet.Fields.Item("DocEntry").Value)
                '  K = oPOR.Comments = "Void PO"
                K = oPOR.Cancel()
                Dim st As String
                PublicVariable.oCompany.GetLastError(K, st)
                If K <> 0 Then
                    'MsgBox(st)
                End If
                oRecordSet.MoveNext()
            Next
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button7_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        'Exit Sub
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                Button6.Enabled = False
                Create_APInvoice()
                '  Button11.Enabled  = True
                'MsgBox("JV  Complected")
            End If
        Else
            Button6.Enabled = False
            Create_APInvoice()
            'MsgBox("JV  Complected")
        End If
    End Sub
    Public Function CardCode_Supplier(ByVal Cardname As String, ByVal Ocompany As SAPbobsCOM.Company) As String
        Dim x As String = ""
        Cardname = Cardname.Replace("'", "''")
        Dim oRecordSet_UN As SAPbobsCOM.Recordset
        oRecordSet_UN = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSet_UN.DoQuery("SELECT T0.[CardCode] FROM OCRD T0  WHERE  T0.[Cardname] ='" & Cardname & "' and T0.CardType='S'")
        x = oRecordSet_UN.Fields.Item(0).Value
        Return x
    End Function
    Private Sub Create_PO()
        Try
            Dim oPOR As SAPbobsCOM.Documents
            oPOR = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseOrders)
            'oPOR = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)

            Dim i As Integer = 0
            Dim K As Integer = 0
            Dim rate As Double = 0
            Dim cost As Double = 0
            Dim st As String = ""
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT  isnull(T1.[Currency],'SGD') 'Currency',T1.Rate,T0.DocCur,T0.[CardName],T0.[DocEntry],T0.[DocDate], T1.[LineNum],isnull(T1.[U_AB_Cost],0) U_AB_Cost,T1.U_AB_Vendor, T1.[ItemCode], T1.[Quantity], T1.[U_AB_Commission] ,isnull(T0.[U_AB_JobNo],'') U_AB_JobNo, T0.[U_AB_Divsion], T0.[U_AB_MAWB], T0.[U_AB_SSIT], T0.[U_AB_HAWB], T0.[U_AB_OriginNameC], T0.[U_AB_DestNameC], T0.[U_AB_TotPkg], T0.[U_AB_TotWT], T0.[U_AB_TotVol], T0.[U_AB_Desc], T0.[U_AB_ETDETA],T0.[U_AB_FLT] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE isnull(T1.[U_St],'')  = '' and isnull(T0.[U_AB_JobNo],'') <> ''")
            For i = 1 To oRecordSet.RecordCount
                Try
                    oPOR = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseOrders)
                    If oRecordSet.Fields.Item("U_AB_JobNo").Value.ToString <> "" Then
                        '           oPOR.DocObjectCode = SAPbobsCOM.BoObjectTypes.oPurchaseOrders
                        oPOR.DocDate = oRecordSet.Fields.Item("DocDate").Value
                        oPOR.DocDueDate = oRecordSet.Fields.Item("DocDate").Value
                        oPOR.TaxDate = oRecordSet.Fields.Item("DocDate").Value
                        oPOR.CardCode = CardCode_Supplier(oRecordSet.Fields.Item("U_AB_Vendor").Value, PublicVariable.oCompany)
                        oPOR.UserFields.Fields.Item("U_AB_JobNo").Value = oRecordSet.Fields.Item("U_AB_JobNo").Value
                        oPOR.UserFields.Fields.Item("U_AB_Divsion").Value = oRecordSet.Fields.Item("U_AB_Divsion").Value.ToString
                        oPOR.UserFields.Fields.Item("U_DocEntry").Value = oRecordSet.Fields.Item("DocEntry").Value.ToString
                        oPOR.UserFields.Fields.Item("U_LineNum").Value = oRecordSet.Fields.Item("LineNum").Value.ToString
                        '--------

                        ' T0.[U_AB_MAWB], T0.[U_AB_SSIT], T0.[U_AB_HAWB], T0.[U_AB_OriginNameC], T0.[U_AB_DestNameC], T0.[U_AB_TotPkg], T0.[U_AB_TotWT], T0.[U_AB_TotVol], T0.[U_AB_Desc], T0.[U_AB_ETDETA]
                        oPOR.UserFields.Fields.Item("U_AB_MAWB").Value = oRecordSet.Fields.Item("U_AB_MAWB").Value
                        oPOR.UserFields.Fields.Item("U_AB_SSIT").Value = oRecordSet.Fields.Item("U_AB_SSIT").Value
                        oPOR.UserFields.Fields.Item("U_AB_HAWB").Value = oRecordSet.Fields.Item("U_AB_HAWB").Value
                        oPOR.UserFields.Fields.Item("U_AB_OriginNameC").Value = oRecordSet.Fields.Item("U_AB_OriginNameC").Value
                        oPOR.UserFields.Fields.Item("U_AB_DestNameC").Value = oRecordSet.Fields.Item("U_AB_DestNameC").Value
                        oPOR.UserFields.Fields.Item("U_AB_TotPkg").Value = oRecordSet.Fields.Item("U_AB_TotPkg").Value
                        oPOR.UserFields.Fields.Item("U_AB_TotWT").Value = oRecordSet.Fields.Item("U_AB_TotWT").Value
                        oPOR.UserFields.Fields.Item("U_AB_TotVol").Value = oRecordSet.Fields.Item("U_AB_TotVol").Value
                        oPOR.UserFields.Fields.Item("U_AB_Desc").Value = oRecordSet.Fields.Item("U_AB_Desc").Value
                        oPOR.UserFields.Fields.Item("U_AB_ETDETA").Value = Format(oRecordSet.Fields.Item("U_AB_ETDETA").Value, "dd/MM/yy")
                        oPOR.UserFields.Fields.Item("U_AB_Consignee").Value = oRecordSet.Fields.Item("CardName").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_FLT").Value = oRecordSet.Fields.Item("U_AB_FLT").Value.ToString
                        'T0.[U_AB_FLT]
                        '----------
                        If oRecordSet.Fields.Item("Currency").Value = "" Then
                            oPOR.Lines.Currency = "SGD"
                        Else
                            oPOR.Lines.Currency = oRecordSet.Fields.Item("Currency").Value
                        End If
                        oPOR.Lines.Rate = oRecordSet.Fields.Item("Rate").Value
                        oPOR.Lines.UnitPrice = oRecordSet.Fields.Item("U_AB_Cost").Value

                        '-----
                        oPOR.Lines.ItemCode = oRecordSet.Fields.Item("ItemCode").Value 'Quantity
                        oPOR.Lines.Quantity = oRecordSet.Fields.Item("Quantity").Value 'Quantity
                        'rate = oRecordSet.Fields.Item("U_AB_Cost").Value
                        'If rate = 0 Then
                        '    cost = oRecordSet.Fields.Item("U_AB_Cost").Value
                        'ElseIf oRecordSet.Fields.Item("DocCur").Value <> "SGD" Then
                        '    cost = oRecordSet.Fields.Item("U_AB_Cost").Value
                        'Else
                        '    cost = oRecordSet.Fields.Item("U_AB_Cost").Value * rate
                        'End If
                        'oPOR.Lines.UnitPrice = cost
                        'oPOR.Lines.TaxType = SAPbobsCOM.BoTaxTypes.tt_No
                        oPOR.Lines.VatGroup = "ZI"

                        oPOR.Lines.Add()
                        Dim str As String = "SELECT U_DocEntry,U_LineNum FROM OPOR T0 WHERE U_DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and U_LineNum='" & oRecordSet.Fields.Item("LineNum").Value.ToString & "'"
                        oRecordSet2.DoQuery(str)
                        If oRecordSet2.RecordCount = 0 Then
                            K = oPOR.Add()
                        End If
                        Dim LineNum As String = oRecordSet.Fields.Item("LineNum").Value
                        If LineNum = "" Then
                            LineNum = 0
                        End If
                        PublicVariable.oCompany.GetLastError(K, st)
                        st = st.Replace("'", " ")
                        If st.Length > 252 Then
                            st = st.Substring(0, 252)
                        End If

                        If K <> 0 Then
                            ''MsgBox(st)
                            oRecordSet1.DoQuery("UPDATE INV1 SET U_St='" & st & "' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & LineNum & "'")
                        Else
                            ' 'MsgBox("Success")
                            oRecordSet1.DoQuery("UPDATE INV1 SET U_St='Success' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & LineNum & "'")
                        End If
                    End If
                    oRecordSet.MoveNext()
                Catch ex As Exception
                    Write(ex.Message)
                    'MsgBox(oRecordSet.Fields.Item("DocEntry").Value.ToString & "-" & st & "-" & ex.Message)
                End Try
            Next
        Catch ex As Exception
            Write(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Create_APInvoice()
        Try
            Dim oPOR As SAPbobsCOM.Documents
            oPOR = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
            Dim i As Integer = 0
            Dim K As Integer = 0
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet21 As SAPbobsCOM.Recordset
            oRecordSet21 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim J As Integer = 0
            Dim NewDocNum As String = ""
            Dim DocNum As String = ""
            Dim JobNo As String = ""
            Dim PVDocNum As String = ""
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[DocEntry],T1.[LineId],T0.[U_VODt],T0.[U_VOVCode], T0.[U_JobNo], T1.[U_ICode], T1.[U_IName],isnull(T1.[U_Qty],1) U_Qty, T1.[U_Amt] FROM [dbo].[@AB_PAYMENTVOUCHER]  T0 , [dbo].[@AB_PAYMENTVOUCHER_L]  T1 WHERE T1.[DocEntry] = T0.[DocEntry] and  isnull(T1.[U_ICode] ,'') <> '' and isnull(T0.U_APSt,'') <> 'Success'")
            For i = 1 To oRecordSet.RecordCount
                Try
                    oPOR = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
                    oPOR.DocDate = oRecordSet.Fields.Item("U_VODt").Value
                    DocNum = oRecordSet.Fields.Item("DocEntry").Value.ToString
                    PVDocNum = oRecordSet.Fields.Item("DocEntry").Value.ToString
                    oPOR.DocObjectCode = SAPbobsCOM.BoObjectTypes.oPurchaseInvoices
                    oPOR.DocDueDate = oRecordSet.Fields.Item("U_VODt").Value
                    oPOR.TaxDate = oRecordSet.Fields.Item("U_VODt").Value
                    If oRecordSet.Fields.Item("U_VOVCode").Value.ToString.Length > 29 Then
                        oPOR.CardCode = oRecordSet.Fields.Item("U_VOVCode").Value.ToString.Substring(0, 30)
                    Else
                        oPOR.CardCode = oRecordSet.Fields.Item("U_VOVCode").Value.ToString
                    End If
                    oPOR.UserFields.Fields.Item("U_DocEntry").Value = oRecordSet.Fields.Item("DocEntry").Value.ToString
                    'oPOR.UserFields.Fields.Item("U_LineNum").Value = oRecordSet.Fields.Item("LineNum").Value.ToString
                    oPOR.UserFields.Fields.Item("U_AB_JobNo").Value = oRecordSet.Fields.Item("U_JobNo").Value.ToString
                    JobNo = oRecordSet.Fields.Item("U_JobNo").Value.ToString
                    If oRecordSet.Fields.Item("U_JobNo").Value.ToString.Substring(0, 2) = "SI" Then
                        oRecordSet2.DoQuery("SELECT T0.[U_QNo], T0.[U_CCode], T0.[U_CName], T0.[U_Atten], T0.[U_ConNo], T0.[U_JobNo], '' [U_RefJob], T0.[U_LoadPortNC], T0.[U_DisPortNC], T0.[U_TotPkg], T0.[U_ChrgWt],T0.U_VessName [U_VessVoyage], T0.[U_OBL], T0.[U_HBL], T0.[U_VessVoyage] [U_F1],T0.[U_GKBNo],T0.[U_VessName],T0.[U_ItemDesc],T0.[U_GrssWt],T0.U_ETA FROM [dbo].[@AB_SEAI_JOB_H] T0   WHERE T0.[U_JobNo]   ='" & JobNo & "'")
                        If oRecordSet2.RecordCount > 0 Then
                            oPOR.UserFields.Fields.Item("U_AB_Divsion").Value = "ZI"
                            oPOR.UserFields.Fields.Item("U_AB_MAWB").Value = oRecordSet2.Fields.Item("U_OBL").Value
                            oPOR.UserFields.Fields.Item("U_AB_SSIT").Value = oRecordSet2.Fields.Item("U_VessName").Value
                            oPOR.UserFields.Fields.Item("U_AB_HAWB").Value = oRecordSet2.Fields.Item("U_HBL").Value
                            oPOR.UserFields.Fields.Item("U_AB_OriginNameC").Value = oRecordSet2.Fields.Item("U_LoadPortNC").Value
                            oPOR.UserFields.Fields.Item("U_AB_DestNameC").Value = oRecordSet2.Fields.Item("U_DisPortNC").Value
                            oPOR.UserFields.Fields.Item("U_AB_TotPkg").Value = oRecordSet2.Fields.Item("U_TotPkg").Value
                            oPOR.UserFields.Fields.Item("U_AB_TotWT").Value = oRecordSet2.Fields.Item("U_ChrgWt").Value
                            oPOR.UserFields.Fields.Item("U_AB_FLT").Value = oRecordSet2.Fields.Item("U_F1").Value
                            oPOR.UserFields.Fields.Item("U_AB_Desc").Value = oRecordSet2.Fields.Item("U_ItemDesc").Value
                            oPOR.UserFields.Fields.Item("U_AB_ETDETA").Value = Format(oRecordSet2.Fields.Item("U_ETA").Value, "dd/MM/yy")
                            oPOR.UserFields.Fields.Item("U_AB_Consignee").Value = oRecordSet2.Fields.Item("U_CName").Value.ToString
                        End If
                    ElseIf oRecordSet.Fields.Item("U_JobNo").Value.ToString.Substring(0, 2) = "SE" Then
                        oRecordSet2.DoQuery("SELECT T0.[U_QNo], T0.[U_CCode], T0.[U_CName], T0.[U_Atten], T0.[U_ConNo], T0.[U_JobNo], '' [U_RefJob], T0.[U_LoadPortNC], T0.[U_DisPortNC], T0.[U_TotPkg], T0.[U_ChrgWt],T0.U_VessName [U_VessVoyage], T0.[U_OBL], T0.[U_HBL], T0.[U_VessVoyage] [U_F1],T0.[U_GKBNo],T0.[U_VessName],T0.[U_GrssWt],T0.U_ItemDesc FROM [dbo].[@AB_SEAE_JOB_H] T0   WHERE T0.[U_JobNo]   ='" & JobNo & "'")
                        If oRecordSet2.RecordCount > 0 Then
                            oPOR.UserFields.Fields.Item("U_AB_Divsion").Value = "SE" 'oRecordSet2.Fields.Item("U_AB_Divsion").Value.ToString
                            oPOR.UserFields.Fields.Item("U_AB_MAWB").Value = oRecordSet2.Fields.Item("U_OBL").Value
                            oPOR.UserFields.Fields.Item("U_AB_SSIT").Value = oRecordSet2.Fields.Item("U_VessName").Value
                            oPOR.UserFields.Fields.Item("U_AB_HAWB").Value = oRecordSet2.Fields.Item("U_HBL").Value
                            oPOR.UserFields.Fields.Item("U_AB_OriginNameC").Value = oRecordSet2.Fields.Item("U_LoadPortNC").Value
                            oPOR.UserFields.Fields.Item("U_AB_DestNameC").Value = oRecordSet2.Fields.Item("U_DisPortNC").Value
                            oPOR.UserFields.Fields.Item("U_AB_TotPkg").Value = oRecordSet2.Fields.Item("U_TotPkg").Value
                            oPOR.UserFields.Fields.Item("U_AB_TotWT").Value = oRecordSet2.Fields.Item("U_GrssWt").Value
                            oPOR.UserFields.Fields.Item("U_AB_Desc").Value = oRecordSet2.Fields.Item("U_ItemDesc").Value
                            'oPOR.UserFields.Fields.Item("U_AB_ETDETA").Value = Format(oRecordSet2.Fields.Item("U_AB_ETDETA").Value, "dd/MM/yy")
                            oPOR.UserFields.Fields.Item("U_AB_Consignee").Value = oRecordSet2.Fields.Item("U_CName").Value.ToString
                        End If
                    ElseIf oRecordSet.Fields.Item("U_JobNo").Value.ToString.Substring(0, 2) = "AI" Then
                        oRecordSet2.DoQuery("SELECT T0.[U_QNo], T0.[U_CCode], T0.[U_CName], T0.[U_Atten], T0.[U_ConNo], T0.[U_JobNo], '' [U_RefJob], T0.[U_LoadPortNC], T0.[U_DisPortNC], T0.[U_TotPkg], T0.[U_ChrgWt],U_VessName [U_VessVoyage],U_AWBNo [U_OBL],U_HAWBNo [U_HBL],U_FNo [U_F1],T0.[U_GKBNo],T0.[U_VessName] FROM [dbo].[@AB_AIRI_JOB_H] T0 WHERE T0.[U_JobNo]   ='" & JobNo & "'")
                        If oRecordSet2.RecordCount > 0 Then
                            oPOR.UserFields.Fields.Item("U_AB_Divsion").Value = "AI" 'oRecordSet2.Fields.Item("U_AB_Divsion").Value.ToString
                            oPOR.UserFields.Fields.Item("U_AB_OriginNameC").Value = oRecordSet2.Fields.Item("U_LoadPortNC").Value.ToString
                            oPOR.UserFields.Fields.Item("U_AB_DestNameC").Value = oRecordSet2.Fields.Item("U_DisPortNC").Value.ToString
                            oPOR.UserFields.Fields.Item("U_AB_TotWT").Value = oRecordSet2.Fields.Item("U_ChrgWt").Value.ToString
                            oPOR.UserFields.Fields.Item("U_AB_SSIT").Value = oRecordSet2.Fields.Item("U_VessName").Value.ToString
                            oPOR.UserFields.Fields.Item("U_AB_MAWB").Value = oRecordSet2.Fields.Item("U_OBL").Value.ToString
                            oPOR.UserFields.Fields.Item("U_AB_HAWB").Value = oRecordSet2.Fields.Item("U_HBL").Value.ToString
                            oPOR.UserFields.Fields.Item("U_AB_FLT").Value = oRecordSet2.Fields.Item("U_F1").Value.ToString
                            oPOR.UserFields.Fields.Item("U_AB_Consignee").Value = oRecordSet2.Fields.Item("U_CName").Value.ToString
                        End If
                    ElseIf oRecordSet.Fields.Item("U_JobNo").Value.ToString.Substring(0, 1) = "AE" Then
                        Dim MAWBNo As String = ""
                        Dim HAWBNo As String = ""
                        oRecordSet2.DoQuery("SELECT T0.[U_AWBNo1] +T0.[U_AWBNo] FROM [dbo].[@AB_AWB_M]  T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
                        MAWBNo = oRecordSet2.Fields.Item(0).Value.ToString
                        oRecordSet2.DoQuery("SELECT T0.[U_HAWBNo] FROM [dbo].[@AB_AWB_H]  T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
                        Dim F As Integer = 0
                        For F = 1 To oRecordSet1.RecordCount
                            If F = 1 Then
                                HAWBNo = oRecordSet1.Fields.Item(0).Value.ToString
                            Else
                                HAWBNo = HAWBNo & "," & oRecordSet1.Fields.Item(0).Value.ToString
                            End If
                            oRecordSet1.MoveNext()
                        Next
                        oRecordSet3.DoQuery("SELECT T1.[U_ChWeight], T1.[U_Pieces], T0.[U_B1], T0.[U_FlighDate1], T0.[U_Nat] FROM [dbo].[@AB_AWB_M]  T0 , [dbo].[@AB_AWB_M2]  T1 WHERE T0.[DocEntry] = T1.[DocEntry] and T0.[U_JobNo]  ='" & JobNo & "'")
                        oRecordSet2.DoQuery("SELECT T0.[U_QNo], T0.[U_CCode], T0.[U_CName], T0.[U_Atten], T0.[U_ConNo], T0.[U_JobNo], '' [U_RefJob], T0.[U_LoadPortNC], T0.[U_DisPortN], T0.[U_TotPkg], T0.[U_ChrgWt],U_VessName [U_VessVoyage],'" & MAWBNo & "' [U_OBL],'" & HAWBNo & "' [U_HBL],U_FNo [U_F1],T0.[U_GKBNo],T0.[U_VessName] FROM [dbo].[@AB_AIRE_JOB_H] T0  WHERE T0.[U_JobNo]   ='" & JobNo & "'")
                        oPOR.UserFields.Fields.Item("U_AB_Divsion").Value = "AE" 'oRecordSet2.Fields.Item("U_AB_Divsion").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_JobNo").Value = oRecordSet2.Fields.Item("U_JobNo").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_OriginNameC").Value = oRecordSet2.Fields.Item("U_LoadPortNC").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_DestNameC").Value = oRecordSet2.Fields.Item("U_DisPortN").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_TotPkg").Value = oRecordSet3.Fields.Item("U_Pieces").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_TotWT").Value = oRecordSet3.Fields.Item("U_ChWeight").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_SSIT").Value = oRecordSet2.Fields.Item("U_VessName").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_MAWB").Value = oRecordSet2.Fields.Item("U_OBL").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_HAWB").Value = oRecordSet2.Fields.Item("U_HBL").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_FLT").Value = oRecordSet3.Fields.Item("U_B1").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_ETDETA").Value = oRecordSet3.Fields.Item("U_FlighDate1").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_Desc").Value = oRecordSet3.Fields.Item("U_Nat").Value.ToString
                        oPOR.UserFields.Fields.Item("U_AB_Consignee").Value = oRecordSet2.Fields.Item("U_CName").Value.ToString
                    ElseIf oRecordSet.Fields.Item("U_JobNo").Value.ToString.Substring(0, 1) = "IN" Then

                    ElseIf oRecordSet.Fields.Item("U_JobNo").Value.ToString.Substring(0, 1) = "PR" Then

                    ElseIf oRecordSet.Fields.Item("U_JobNo").Value.ToString.Substring(0, 1) = "LO" Then

                    End If



                    '                oPOR.UserFields.Fields.Item("U_AB_Divsion").Value = oRecordSet.Fields.Item("U_AB_Divsion").Value.ToString
                    For J = 1 To 500
                        'oPOR.Lines.UserFields.Fields.Item("U_DocEntry").Value = oRecordSet.Fields.Item("DocEntry").Value.ToString
                        'oPOR.Lines.UserFields.Fields.Item("U_LineNum").Value = oRecordSet.Fields.Item("LineId").Value.ToString
                        oPOR.Lines.ItemCode = oRecordSet.Fields.Item("U_ICode").Value
                        oPOR.Lines.Quantity = oRecordSet.Fields.Item("U_Qty").Value '"1.0"
                        oPOR.Lines.UnitPrice = oRecordSet.Fields.Item("U_Amt").Value
                        oPOR.Lines.VatGroup = "ZI"
                        oPOR.Lines.Add()
                        If oRecordSet.BoF <> False Then
                            oRecordSet.MoveNext()
                        Else
                            Exit For
                        End If
                        NewDocNum = oRecordSet.Fields.Item("DocEntry").Value.ToString
                        If NewDocNum <> DocNum Then
                            Exit For
                        End If
                    Next
                    Dim str As String = "Select U_APSt from [@AB_PAYMENTVOUCHER] where [U_APSt]='Success' and DocEntry='" & PVDocNum & "'"
                    oRecordSet21.DoQuery(str)
                    If oRecordSet21.RecordCount = 0 Then
                        K = oPOR.Add


                    End If
                    Dim K1 As String
                    K1 = PublicVariable.oCompany.GetNewObjectKey()
                    Dim st As String = ""
                    PublicVariable.oCompany.GetLastError(K, st)
                    If K <> 0 Then
                        oRecordSet1.DoQuery("UPDATE [@AB_PAYMENTVOUCHER] SET [U_APSt]='" & st & "' where DocEntry='" & PVDocNum & "'")
                    Else
                        oRecordSet1.DoQuery("UPDATE [@AB_PAYMENTVOUCHER] SET [U_APSt]='Success' where DocEntry='" & PVDocNum & "'")
                    End If
                    'oRecordSet.MoveNext()

                Catch ex As Exception
                    WriteLog(ex.Message & "-" & i)
                    'oRecordSet.MoveNext()
                End Try
            Next
        Catch ex As Exception
            WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Button9_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        'Exit Sub
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                Button6.Enabled = False
                Create_PO()
                '  Button11.Enabled  = True
                'MsgBox("JV  Complected")
            End If
        Else
            Button6.Enabled = False
            Create_PO()
            'MsgBox("JV  Complected")
        End If
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Exit Sub
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                Button6.Enabled = False
                Closed1()
                '  Button11.Enabled  = True
                'MsgBox("JV  Complected")
            End If
        Else
            Button6.Enabled = False
            Closed1()
            'MsgBox("JV  Complected")
        End If
    End Sub

    Public Sub Closed1()
        Try


            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT T0.[U_AB_JobNo] FROM ODRF T0 WHERE isnull(T0.[U_AB_JobNo],'') <> '' and T0.[ObjType] ='13'")
            Dim i As Integer
            Dim JobNo As String = ""
            For i = 1 To oRecordSet1.RecordCount
                JobNo = oRecordSet1.Fields.Item("U_AB_JobNo").Value
                If JobNo.Substring(0, 2) = "SI" Then
                    oRecordSet2.DoQuery("SELECT T0.[U_QNo] FROM [dbo].[@AB_SEAI_JOB_H]  T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
                    ''MsgBox(oRecordSet2.Fields.Item("U_QNo").Value)
                    oRecordSet3.DoQuery("update ORDR Set U_AB_POD='Closed'  where DocNum=" & oRecordSet2.Fields.Item("U_QNo").Value & "")
                ElseIf JobNo.Substring(0, 2) = "SE" Then
                    oRecordSet2.DoQuery("SELECT T0.[U_QNo] FROM [dbo].[@AB_SEAE_JOB_H]  T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
                    oRecordSet3.DoQuery("update ORDR Set U_AB_POD='Closed'  where DocNum='" & oRecordSet2.Fields.Item("U_QNo").Value & "'")
                ElseIf JobNo.Substring(0, 2) = "AI" Then
                    oRecordSet2.DoQuery("SELECT T0.[U_QNo] FROM [dbo].[@AB_AIRI_JOB_H]  T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
                    oRecordSet3.DoQuery("update ORDR Set U_AB_POD='Closed'  where DocNum='" & oRecordSet2.Fields.Item("U_QNo").Value & "'")
                ElseIf JobNo.Substring(0, 2) = "AE" Then
                    oRecordSet2.DoQuery("SELECT T0.[U_QNo] FROM [dbo].[@AB_AIRE_JOB_H]  T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
                    oRecordSet3.DoQuery("update ORDR Set U_AB_POD='Closed'  where DocNum='" & oRecordSet2.Fields.Item("U_QNo").Value & "'")
                ElseIf JobNo.Substring(0, 2) = "IN" Then
                    oRecordSet2.DoQuery("SELECT T0.[U_QNo] FROM [dbo].[@AB_INT_HEADER]  T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
                    oRecordSet3.DoQuery("update ORDR Set U_AB_POD='Closed'  where DocNum='" & oRecordSet2.Fields.Item("U_QNo").Value & "'")
                ElseIf JobNo.Substring(0, 2) = "PR" Then
                    oRecordSet2.DoQuery("SELECT T0.[U_QNo] FROM [dbo].[@AB_PRO_HEADER]  T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
                    oRecordSet3.DoQuery("update ORDR Set U_AB_POD='Closed'  where DocNum='" & oRecordSet2.Fields.Item("U_QNo").Value & "'")
                ElseIf JobNo.Substring(0, 2) = "LO" Then
                    oRecordSet2.DoQuery("SELECT T0.[U_QNo] FROM [dbo].[@AB_LOCAL_HEADER]  T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
                    oRecordSet3.DoQuery("update ORDR Set U_AB_POD='Closed'  where DocNum='" & oRecordSet2.Fields.Item("U_QNo").Value & "'")
                End If
                oRecordSet1.MoveNext()
            Next
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Exit Sub
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                Button6.Enabled = False
                Update_PO()
                '  Button11.Enabled  = True
                'MsgBox("JV  Complected")
            End If
        Else
            Button6.Enabled = False
            Update_PO()
            'MsgBox("JV  Complected")
        End If
    End Sub
    Public Sub Update_PO()
        'SELECT T1.[DocEntry], isnull(T1.[LineNum],0) LineNum, isnull(T1.[Currency],'SGD') 'Currency', T1.[Rate], isnull(T1.[U_AB_Cost],0.00) U_AB_Cost  FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry  where T1.[DocEntry]='1' and T1.[LineNum]='0'

        Try


            Dim oPOR As SAPbobsCOM.Documents
            oPOR = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseOrders)
            Dim i As Integer = 0
            Dim K As Integer = 0
            Dim rate As Double = 0
            Dim cost As Double = 0
            Dim st As String = ""
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.DocEntry,T0.[U_DocEntry], T0.[U_LineNum] FROM OPOR T0 WHERE T0.DocStatus='O' and T0.DocEntry>'4450' and Isnull(U_DocEntry,'')<> '' ORDER BY T0.[DocNum]")
            Dim DocEntry As String = ""
            Dim LineNum As String = ""
            Dim PODocNum As String = ""
            Dim str As String = ""

            For i = 1 To oRecordSet.RecordCount
                Try
                    DocEntry = oRecordSet.Fields.Item("U_DocEntry").Value
                    LineNum = oRecordSet.Fields.Item("U_LineNum").Value
                    str = "SELECT T1.[DocEntry], isnull(T1.[LineNum],0) LineNum, isnull(T1.[Currency],'SGD') 'Currency', T1.[Rate], isnull(T1.[U_AB_Cost],0.00) U_AB_Cost  FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry  where T1.[DocEntry]='" & DocEntry & "' and T1.[LineNum]='" & LineNum & "'"
                    oRecordSet2.DoQuery(str)
                    PODocNum = oRecordSet.Fields.Item("DocEntry").Value
                    If PODocNum <> "" Then
                        oPOR.GetByKey(PODocNum)
                        oPOR.Lines.SetCurrentLine(0)
                        If oRecordSet2.Fields.Item("Currency").Value = "" Then
                            oPOR.Lines.Currency = "SGD"
                        Else
                            oPOR.Lines.Currency = oRecordSet2.Fields.Item("Currency").Value
                        End If
                        oPOR.Lines.Rate = oRecordSet2.Fields.Item("Rate").Value
                        oPOR.Lines.UnitPrice = oRecordSet2.Fields.Item("U_AB_Cost").Value
                        oPOR.Lines.Add()
                        K = oPOR.Update
                        PublicVariable.oCompany.GetLastError(K, st)
                        st = st.Replace("'", " ")
                        If st.Length > 252 Then
                            st = st.Substring(0, 252)
                        End If
                        If K <> 0 Then
                            WriteLog("PO No:" & PODocNum & "Error Message" & st)
                            'oRecordSet1.DoQuery("UPDATE INV1 SET U_St='" & st & "' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & LineNum & "'")
                        Else
                            ' oRecordSet1.DoQuery("UPDATE INV1 SET U_St='Success' where DocEntry='" & oRecordSet.Fields.Item("DocEntry").Value & "' and LineNum='" & LineNum & "'")
                        End If
                    End If
                    oRecordSet.MoveNext()
                Catch ex As Exception
                    'MsgBox(ex.Message)
                End Try
            Next
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try

    End Sub

    Public Sub WriteLog(ByVal Str As String)
        Dim oWrite As IO.StreamWriter
        Dim FilePath As String
        FilePath = Application.StartupPath + "\ErrorLog.txt"

        If IO.File.Exists(FilePath) Then
            oWrite = IO.File.AppendText(FilePath)
        Else
            oWrite = IO.File.CreateText(FilePath)
        End If
        oWrite.Write(Now.ToString() + ":" + Str + vbCrLf)
        oWrite.Close()
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        Exit Sub
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                Button6.Enabled = False
                Update_APInvoice()
                '  Button11.Enabled  = True
                'MsgBox("JV  Complected")
            End If
        Else
            Button6.Enabled = False
            Update_APInvoice()
            'MsgBox("JV  Complected")
        End If
    End Sub


    Private Sub Update_APInvoice()
        Try
            Dim oPOR As SAPbobsCOM.Documents
            oPOR = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
            Dim i As Integer = 0
            Dim K As Integer = 0
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim J As Integer = 0
            Dim NewDocNum As String = ""
            Dim DocNum As String = ""
            Dim JobNo As String = ""
            Dim PVDocNum As String = ""
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T1.[DocEntry] FROM ODRF T0  INNER JOIN DRF1 T1 ON T0.DocEntry = T1.DocEntry WHERE T0.[ObjType] ='18'")
            For i = 1 To oRecordSet.RecordCount
                oPOR = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)

                DocNum = oRecordSet.Fields.Item("DocEntry").Value.ToString
                oPOR.GetByKey(DocNum)
                For J = 0 To 500
                    oPOR.Lines.SetCurrentLine(J)
                    'oPOR.Lines.TaxType = SAPbobsCOM.BoTaxTypes.tt_No
                    'oPOR.Lines.TaxPercentagePerRow = 0
                    'oPOR.Lines.TaxTotal = 0
                    'oPOR.Lines.TaxLiable = SAPbobsCOM.BoYesNoEnum.tNO
                    'oPOR.Lines.TaxOnly = SAPbobsCOM.BoYesNoEnum.tNO
                    oPOR.Lines.VatGroup = "ZI"


                    oPOR.Lines.Add()
                    oRecordSet.MoveNext()
                    NewDocNum = oRecordSet.Fields.Item("DocEntry").Value.ToString
                    If NewDocNum <> DocNum Then
                        Exit For
                    End If
                Next

                K = oPOR.Update
                Dim K1 As String
                K1 = PublicVariable.oCompany.GetNewObjectKey()
                Dim st As String = ""

                PublicVariable.oCompany.GetLastError(K, st)
                If K <> 0 Then
                    'MsgBox(DocNum & "-" & st)
                    '   oRecordSet1.DoQuery("UPDATE [@AB_PAYMENTVOUCHER] SET [U_APSt]='" & st & "' where DocEntry='" & PVDocNum & "'")
                Else
                    '  oRecordSet1.DoQuery("UPDATE [@AB_PAYMENTVOUCHER] SET [U_APSt]='Success' where DocEntry='" & PVDocNum & "'")
                End If
                'oRecordSet.MoveNext()
            Next
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub UpdateMAWBNO()
        Try
            Dim i As Integer = 0
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT isnull(T0.[U_AWBNo1],'') +'-'+ isnull(T0.[U_AWBNo2],'') +'-'+ isnull(T0.[U_AWBNo] ,'') AWBNo ,T0.[U_JobNo]  FROM [dbo].[@AB_AWB_M]  T0 ")
            For i = 1 To oRecordSet1.RecordCount
                oRecordSet2.DoQuery("Update [dbo].[@AB_AIRE_JOB_H]  set [U_AWBNo] ='" & oRecordSet1.Fields.Item(0).Value.ToString & "' where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "'")
                oRecordSet1.MoveNext()
            Next
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT isnull(T0.[U_HAWBNo],'') AWBNo ,T0.[U_JobNo]  FROM [dbo].[@AB_AWB_H]  T0 ")
            For i = 1 To oRecordSet1.RecordCount
                oRecordSet3.DoQuery("select U_HAWBNo from [dbo].[@AB_AIRE_JOB_H] where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "' and isnull(U_HAWBNo,'') <> ''")
                If oRecordSet3.RecordCount > 0 Then
                    Dim hawbno As String = oRecordSet3.Fields.Item(0).Value.ToString & "," & oRecordSet1.Fields.Item(0).Value.ToString
                    oRecordSet2.DoQuery("Update [dbo].[@AB_AIRE_JOB_H]  set [U_HAWBNo] ='" & hawbno & "' where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "'")
                Else
                    oRecordSet2.DoQuery("Update [dbo].[@AB_AIRE_JOB_H]  set [U_HAWBNo] ='" & oRecordSet1.Fields.Item(0).Value.ToString & "' where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "'")
                End If

                oRecordSet1.MoveNext()
            Next
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        Exit Sub
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                'MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                Button6.Enabled = False
                UpdateMAWBNO()
                '  Button11.Enabled  = True
                'MsgBox("JV  Complected")
            End If
        Else
            Button6.Enabled = False
            UpdateMAWBNO()
            'MsgBox("JV  Complected")
        End If
    End Sub
    Private Sub UPDATE_FTP_WHSC()
        Try
            '
            Dim i As Integer = 0
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT DOCENTRY,u_pATH,u_sT FROM [@aigr2] WHERE u_PATH IS NOT NULL AND ISNULL(CAST(u_pATH AS VARCHAR(2000)),'') <> '\\gksap\F\'")
            Dim sourcePath As String = ""
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT AttachPath from OADP")
            Dim destPath As String = oRecordSet.Fields.Item("AttachPath").Value.ToString
            If Not Directory.Exists(destPath) Then
                Exit Sub
            End If

            Try
                oRecordSet1.MoveFirst()
                For i = 1 To oRecordSet1.RecordCount
                    Try
                        oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        If oRecordSet1.Fields.Item(1).Value.ToString <> "" Then
                            sourcePath = oRecordSet1.Fields.Item(1).Value.ToString
                            If sourcePath.Contains(destPath) = False Then
                                Dim FileName As String = Path.GetFileNameWithoutExtension(sourcePath) '& Now.ToString("ddMMyyyyhhmmssffff")
                                Dim FileExten As String = Path.GetExtension(sourcePath)
                                Dim path1 As String = Path.GetDirectoryName(sourcePath)
                                'path1 = path1 & "\"
                                MsgBox(i)
                                If path1 <> destPath Then
                                    MsgBox("test" - i)
                                    Dim K As Integer = 1
100:                                If System.IO.File.Exists(destPath & FileName & FileExten) Then
                                        ' MsgBox("THis Name Existsts")
                                        FileName = Path.GetFileNameWithoutExtension(sourcePath) & "_" & K
                                        K = K + 1
                                        GoTo 100
                                    End If
                                    Dim dest As String = Path.Combine(destPath, FileName & FileExten)
                                    File.Copy(sourcePath, dest, False)
                                    oRecordSet2.DoQuery("Update [dbo].[@aigr2]  set [u_pATH] ='" & destPath & "',[u_sT] ='Closed' where DocEntry=" & oRecordSet1.Fields.Item(0).Value & "")
                                End If
                            End If
                        End If
                        ' oRecordSet2.DoQuery("Update [dbo].[@AB_AIRE_JOB_H]  set [U_AWBNo] ='" & oRecordSet1.Fields.Item(0).Value.ToString & "' where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "'")

                    Catch ex As Exception
                        WriteLog(ex.Message)
                    End Try
                    oRecordSet1.MoveNext()
                Next

                '                oRecordSet1.MoveFirst()
                '                For i = 1 To oRecordSet1.RecordCount
                '                    Try
                '                        If oRecordSet1.Fields.Item(2).Value.ToString <> "" Then
                '                            sourcePath = oRecordSet1.Fields.Item(2).Value.ToString
                '                            If sourcePath.Contains(destPath) = False Then
                '                                Dim FileName As String = Path.GetFileNameWithoutExtension(sourcePath) '& Now.ToString("ddMMyyyyhhmmssffff")
                '                                Dim FileExten As String = Path.GetExtension(sourcePath)
                '                                Dim path1 As String = Path.GetDirectoryName(sourcePath)
                '                                'path1 = path1 & "\"
                '                                If path1 <> destPath Then
                '                                    Dim K As Integer = 1
                '10:                                 If System.IO.File.Exists(destPath & FileName & FileExten) Then
                '                                        ' MsgBox("THis Name Existsts")
                '                                        FileName = Path.GetFileNameWithoutExtension(sourcePath) & "_" & K
                '                                        K = K + 1
                '                                        GoTo 10
                '                                    End If
                '                                    Dim dest As String = Path.Combine(destPath, FileName & FileExten)
                '                                    File.Copy(sourcePath, dest, False)
                '                                    oRecordSet2.DoQuery("Update [dbo].[ORCT]  set [U_Att2] ='" & dest & "' where DocEntry='" & oRecordSet1.Fields.Item(0).Value.ToString & "'")
                '                                End If
                '                            End If
                '                        End If
                ' oRecordSet2.DoQuery("Update [dbo].[@AB_AIRE_JOB_H]  set [U_AWBNo] ='" & oRecordSet1.Fields.Item(0).Value.ToString & "' where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "'")

                'Catch ex As Exception
                '    WriteLog(ex.Message)
                'End Try
                'oRecordSet1.MoveNext()
                '    Next

            Catch ex As Exception
                WriteLog(ex.Message)
            End Try
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub UPDATE_FTP()
        Try
            '
            Dim i As Integer = 0
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT t0.DocEntry, isnull( T0.[U_Att1],'') 'Attachment1', isnull(T0.[U_Att2],'') 'Attachment2' FROM ORCT T0 where  cast(isnull( T0.[U_Att1],'') as Varchar(1500))<> '' or  cast(isnull( T0.[U_Att2],'') as Varchar(1500))<> ''")
            Dim sourcePath As String = ""
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT AttachPath from OADP")
            Dim destPath As String = oRecordSet.Fields.Item("AttachPath").Value.ToString
            If Not Directory.Exists(destPath) Then
                Exit Sub
            End If

            Try
                oRecordSet1.MoveFirst()
                For i = 1 To oRecordSet1.RecordCount
                    Try
                        oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        If oRecordSet1.Fields.Item(1).Value.ToString <> "" Then
                            If oRecordSet1.Fields.Item(0).Value = 348 Then
                                MsgBox("ok0")
                            End If
                            sourcePath = oRecordSet1.Fields.Item(1).Value.ToString
                            If sourcePath.Contains(destPath) = False Then
                                Dim FileName As String = Path.GetFileNameWithoutExtension(sourcePath) '& Now.ToString("ddMMyyyyhhmmssffff")
                                Dim FileExten As String = Path.GetExtension(sourcePath)
                                Dim path1 As String = Path.GetDirectoryName(sourcePath)
                                'path1 = path1 & "\"
                                If oRecordSet1.Fields.Item(0).Value = 348 Then
                                    MsgBox("ok1")
                                End If

                                If path1 <> destPath Then
                                    Dim K As Integer = 1
100:                                If System.IO.File.Exists(destPath & FileName & FileExten) Then
                                        ' MsgBox("THis Name Existsts")
                                        FileName = Path.GetFileNameWithoutExtension(sourcePath) & "_" & K
                                        K = K + 1
                                        GoTo 100
                                    End If
                                    If oRecordSet1.Fields.Item(0).Value = 348 Then
                                        MsgBox("ok2")
                                    End If
                                    Dim dest As String = Path.Combine(destPath, FileName & FileExten)
                                    File.Copy(sourcePath, dest, False)
                                    If oRecordSet1.Fields.Item(0).Value = 348 Then
                                        MsgBox("ok3")
                                    End If
                                    oRecordSet2.DoQuery("Update [dbo].[ORCT]  set [U_Att1] ='" & dest & "' where DocEntry=" & oRecordSet1.Fields.Item(0).Value & "")

                                End If
                            End If
                        End If
                        ' oRecordSet2.DoQuery("Update [dbo].[@AB_AIRE_JOB_H]  set [U_AWBNo] ='" & oRecordSet1.Fields.Item(0).Value.ToString & "' where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "'")

                    Catch ex As Exception
                        WriteLog(ex.Message)
                    End Try
                    oRecordSet1.MoveNext()
                Next

                oRecordSet1.MoveFirst()
                For i = 1 To oRecordSet1.RecordCount
                    Try
                        If oRecordSet1.Fields.Item(2).Value.ToString <> "" Then
                            sourcePath = oRecordSet1.Fields.Item(2).Value.ToString
                            If sourcePath.Contains(destPath) = False Then
                                Dim FileName As String = Path.GetFileNameWithoutExtension(sourcePath) '& Now.ToString("ddMMyyyyhhmmssffff")
                                Dim FileExten As String = Path.GetExtension(sourcePath)
                                Dim path1 As String = Path.GetDirectoryName(sourcePath)
                                'path1 = path1 & "\"
                                If path1 <> destPath Then
                                    Dim K As Integer = 1
10:                                 If System.IO.File.Exists(destPath & FileName & FileExten) Then
                                        ' MsgBox("THis Name Existsts")
                                        FileName = Path.GetFileNameWithoutExtension(sourcePath) & "_" & K
                                        K = K + 1
                                        GoTo 10
                                    End If
                                    Dim dest As String = Path.Combine(destPath, FileName & FileExten)
                                    File.Copy(sourcePath, dest, False)
                                    oRecordSet2.DoQuery("Update [dbo].[ORCT]  set [U_Att2] ='" & dest & "' where DocEntry='" & oRecordSet1.Fields.Item(0).Value.ToString & "'")
                                End If
                            End If
                        End If
                        ' oRecordSet2.DoQuery("Update [dbo].[@AB_AIRE_JOB_H]  set [U_AWBNo] ='" & oRecordSet1.Fields.Item(0).Value.ToString & "' where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "'")

                    Catch ex As Exception
                        WriteLog(ex.Message)
                    End Try
                    oRecordSet1.MoveNext()
                Next

            Catch ex As Exception
                WriteLog(ex.Message)
            End Try
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub UPDATE_FTP1()
        Try
            '
            Dim i As Integer = 0
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet1 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT t0.DocEntry, isnull( T0.[U_Att1],'') 'Attachment1', isnull(T0.[U_Att2],'') 'Attachment2' FROM OVPM T0 where  cast(isnull( T0.[U_Att1],'') as Varchar(1500))<> '' or  cast(isnull( T0.[U_Att2],'') as Varchar(1500))<> ''")
            Dim sourcePath As String = ""
            oRecordSet = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT AttachPath from OADP")
            Dim destPath As String = oRecordSet.Fields.Item("AttachPath").Value.ToString
            If Not Directory.Exists(destPath) Then
                Exit Sub
            End If
            Try
                For i = 1 To oRecordSet1.RecordCount
                    Try
                        If oRecordSet1.Fields.Item(1).Value.ToString <> "" Then
                            sourcePath = oRecordSet1.Fields.Item(1).Value.ToString
                            If sourcePath.Contains(destPath) = False Then
                                Dim FileName As String = Path.GetFileNameWithoutExtension(sourcePath) '& Now.ToString("ddMMyyyyhhmmssffff")
                                Dim FileExten As String = Path.GetExtension(sourcePath)
                                Dim path1 As String = Path.GetDirectoryName(sourcePath)
                                'path1 = path1 & "\"
                                If path1 <> destPath Then
                                    Dim K As Integer = 1
100:                                If System.IO.File.Exists(destPath & FileName & FileExten) Then
                                        ' MsgBox("THis Name Existsts")
                                        FileName = Path.GetFileNameWithoutExtension(sourcePath) & "_" & K
                                        K = K + 1
                                        GoTo 100
                                    End If
                                    Dim dest As String = Path.Combine(destPath, FileName & FileExten)
                                    File.Copy(sourcePath, dest, False)
                                    oRecordSet2.DoQuery("Update [dbo].[OVPM]  set [U_Att1] ='" & dest & "' where DocEntry='" & oRecordSet1.Fields.Item(0).Value.ToString & "'")
                                End If
                            End If
                        End If
                        ' oRecordSet2.DoQuery("Update [dbo].[@AB_AIRE_JOB_H]  set [U_AWBNo] ='" & oRecordSet1.Fields.Item(0).Value.ToString & "' where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "'")

                    Catch ex As Exception
                        WriteLog(ex.Message)
                    End Try
                    oRecordSet1.MoveNext()
                Next

                oRecordSet1.MoveFirst()
                For i = 1 To oRecordSet1.RecordCount
                    Try
                        If oRecordSet1.Fields.Item(2).Value.ToString <> "" Then
                            sourcePath = oRecordSet1.Fields.Item(2).Value.ToString
                            If sourcePath.Contains(destPath) = False Then
                                Dim FileName As String = Path.GetFileNameWithoutExtension(sourcePath) '& Now.ToString("ddMMyyyyhhmmssffff")
                                Dim FileExten As String = Path.GetExtension(sourcePath)
                                Dim path1 As String = Path.GetDirectoryName(sourcePath)
                                'path1 = path1 & "\"
                                If path1 <> destPath Then
                                    Dim K As Integer = 1
10:                                 If System.IO.File.Exists(destPath & FileName & FileExten) Then
                                        ' MsgBox("THis Name Existsts")
                                        FileName = Path.GetFileNameWithoutExtension(sourcePath) & "_" & K
                                        K = K + 1
                                        GoTo 10
                                    End If
                                    Dim dest As String = Path.Combine(destPath, FileName & FileExten)
                                    File.Copy(sourcePath, dest, False)
                                    oRecordSet2.DoQuery("Update [dbo].[OVPM]  set [U_Att2] ='" & dest & "' where DocEntry='" & oRecordSet1.Fields.Item(0).Value.ToString & "'")
                                End If
                            End If
                        End If
                        ' oRecordSet2.DoQuery("Update [dbo].[@AB_AIRE_JOB_H]  set [U_AWBNo] ='" & oRecordSet1.Fields.Item(0).Value.ToString & "' where U_JobNo='" & oRecordSet1.Fields.Item(1).Value.ToString & "'")

                    Catch ex As Exception
                        WriteLog(ex.Message)
                    End Try
                    oRecordSet1.MoveNext()
                Next

            Catch ex As Exception
                WriteLog(ex.Message)
            End Try
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        'Exit Sub
        If MsgBox("Do you want to Proceed!", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If
        If Connection.bConnect = False Then
            connect.setDB()
            If Not connect.connectDB() Then
                MsgBox("SAP Connection Failed")
                Exit Sub
            Else
                Button6.Enabled = False
                'UPDATE_FTP()
                'UPDATE_FTP1()
                UPDATE_FTP_WHSC()
                '  Button11.Enabled  = True
                MsgBox("Complected")
            End If
        Else
            Button6.Enabled = False
            'UPDATE_FTP()
            'UPDATE_FTP1()
            UPDATE_FTP_WHSC()
            MsgBox("Complected")
        End If
    End Sub
End Class
